逆向工程类
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
配置文件
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip"/>-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<!-- 是否取消注释 -->
<property name="suppressAllComments" value="true"/>
<!-- 是否生成注释带时间戳-->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- mysql数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db_xxx?serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 指定JavaBean生成的位置 -->
<javaModelGenerator targetPackage="com.xxx.xxx.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--指定sql映射文件生成的位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--指定dao接口生成的位置,mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.xxx.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 需要生成的实体类对应的表名,多个实体类复制多份该配置即可 -->
<!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 -->
<table tableName="SYS_USER" domainObjectName="User"/>
</context>
</generatorConfiguration>
上述配置需要将generatorConfig.xml放在项目的根目录下