首先配置class执行文件
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 java.io.File;
import java.util.ArrayList;
import java.util.List;
public class GeneratorSqlmap {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件 最好给与绝对地址
File configFile = new File("D:\\projects\\Tide\\src\\main\\resources\\GeneratorConfig.xml");
System.out.println(configFile.getAbsolutePath());
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);
}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置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>
<properties resource="jdbc.properties"></properties>
<classPathEntry
location="${jdbc.driverLocation}" />
<context id="context1">
<!--<commentGenerator> <!– 去除自动生成的注释 –> <property name="suppressAllComments"
value="true" /> </commentGenerator> -->
<!-- 是否生成注释 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接配置 -->
<jdbcConnection
driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}" />
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--配置生成的实体包 targetPackage:生成的实体包位置,默认存放在src目录下 targetProject:目标工程名 -->
<javaModelGenerator targetPackage="com.pxzl.pojo"
targetProject="绝对地址">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->
<sqlMapGenerator targetPackage="com.pxzl.mapper"
targetProject="绝对地址">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成Dao类存放位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.pxzl.mapper" targetProject="绝对地址">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--生成对应表及类名 -->
<!-- 配置表 schema:不用填写 tableName: 表名 enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
去除自动生成的例子 -->
<table schema="" tableName="case"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" enableDeleteByPrimaryKey="true"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" />
</context>
</generatorConfiguration>
配置jdbc文件
#数据库配置文件
jdbc.driverLocation=D:\\SQL\\sqljdbc4-2.0.jar
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://127.0.0.1:1433;databasename=Tide
jdbc.username=
jdbc.password=
执行成功最后