MyBatis 根据数据表反向生成 java 实体类等
1、MyBatis 根据已有的数据表生成对应的 Java 实体类的配置文件:
mybatisReverseGeneratorConfig.xml
此文件放到项目的根目录下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 7 <context id="GBaseContext" targetRuntime="MyBatis3" defaultModelType="flat"> 8 9 <!--前置分隔符--> 10 <property name="beginningDelimiter" value="`"/> 11 <!--后置分隔符--> 12 <property name="endingDelimiter" value="`"/> 13 <!--设置编码--> 14 <property name="javaFileEncoding" value="UTF-8"/> 15 <!--生成mapper.xml时覆盖原文件--> 16 <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> 17 18 <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" 19 connectionURL="jdbc:mysql://127.0.0.1:3309/db_demo" 20 userId="admin" 21 password="123456"> 22 </jdbcConnection> 23 24 <javaModelGenerator targetPackage="com.smbea.po.generated" targetProject="src/main/java"> 25 <property name="enableSubPackages" value="true"/> 26 <property name="trimStrings" value="true"/> 27 </javaModelGenerator> 28 29 <!-- 反向生成持久化对象(*PO) --> 30 <sqlMapGenerator targetPackage="com.smbea.mapper.mysql.generated" targetProject="src/main/resources"> 31 <property name="enableSubPackages" value="true"/> 32 </sqlMapGenerator> 33 34 <!-- 反向生成 Mapper 接口(*Mapper) --> 35 <javaClientGenerator type="XMLMAPPER" targetPackage="com.smbea.mapper.mysql.generated" targetProject="src/main/java"> 36 <property name="enableSubPackages" value="true"/> 37 </javaClientGenerator> 38 39 <!-- <table tableName="%"></table>--> <!-- % 表示全部表 --> 41 <table tableName="t_user" domainObjectName="User"></table> 42 <table tableName="t_order" domainObjectName="Order"></table> 43 <table tableName="t_trolly" domainObjectName="Trolly"></table> 50 51 </context> 52 53 </generatorConfiguration>
2、在项目的 pom.xml 文件加入如下依赖:
1 <dependency> 2 <groupId>org.mybatis.generator</groupId> 3 <artifactId>mybatis-generator-core</artifactId> 4 <version>1.4.0</version> 5 </dependency>
3、反向生成入口类:
1 package com.smbea.tool; 2 3 import org.mybatis.generator.api.MyBatisGenerator; 4 import org.mybatis.generator.config.Configuration; 5 import org.mybatis.generator.config.xml.ConfigurationParser; 6 import org.mybatis.generator.internal.DefaultShellCallback; 7 8 import java.io.File; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 /** 13 * MyBatis 反向工程生成器 14 */ 15 public class MyBatisReverseGenerator { 16 17 public static void main(String[] args) throws Exception { 18 List<String> warnings = new ArrayList<>(); 19 boolean overwrite = true; 20 File configFile = new File("mybatisReverseGeneratorConfig.xml"); 21 ConfigurationParser cp = new ConfigurationParser(warnings); 22 Configuration config = cp.parseConfiguration(configFile); 23 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 24 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 25 myBatisGenerator.generate(null); 26 } 27 28 }
标签:实体类,java,generator,org,数据表,new,mybatis,import From: https://www.cnblogs.com/hapday/p/18344585