1、配置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>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否取消自动生成的注释true:是false:否 -->
<property name="suppressAllComments" value="true"/>
<!--设置数据库连接参数-->
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"
userId=""
password="">
**<!-- 非常重要 不设置会导致mybatis尝试为所有数据库中的同名表生成实体类及.xml-->**
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 默认false,把jdbc decimal和numberic类型解析为Integer,为true把jdbc decimal和numberic类型解析为java BigDecimal -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成 pojo的位置 -->
<javaModelGenerator targetPackage="test.model" targetProject=".\src">
<!-- 是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值域清理空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper.xml文件的位置 -->
<sqlMapGenerator targetPackage="test.xml" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mapper接口的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="user"/>
<table tableName="orders" >
</table>
<!-- 为数据库表指定字段设置java数据类型 按需设置 -->
<!-- <columnOverride column="number" javaType="String"/>-->
</context>
</generatorConfiguration>
2、main方法
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定配置文件 其余不用修改
File configFile = new File("./config/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
try {
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}