1、引入jar
2、配置mybaits.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="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
<property name="searchString" value="Example$" />
<property name="replaceString" value="Criteria" />
</plugin>
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://172.16.8.61:3306/tbcmsdb"
userId="tbcmsowner" password="ce#%^MZY45uauoMh">
</jdbcConnection>
<javaModelGenerator targetPackage="com.bonc.cms.dao.mapper.bo"
targetProject="unicom-cms-coresrcmainjava">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mybatis.mapper.com"
targetProject="unicom-cms-coresrcmain esources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.bonc.cms.dao.mapper.interfaces"
targetProject="unicom-cms-coresrcmainjava">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="test" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
3、java主函数
package com.bonc.cms.code;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 生成mybatis文件
*
* @author mayt
*/
public class CodeGeneratorMain {
private static final Logger LOGGER = LoggerFactory.getLogger(CodeGeneratorMain.class);
/** 配置文件路径 */
public static final String CFG_FILE_PATH = CodeGeneratorMain.class
.getResource("/mybatis/generator/generatorConfig.xml").getFile();
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
File configFile = new File(CFG_FILE_PATH);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
for (String warning : warnings) {
System.out.println(warning);
}
LOGGER.info("gen end");
}
}