逆向工程核心包
mybatis-generator-core-1.3.2-javadoc.jar
mybatis-generator-core-1.3.2-sources.jar
mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.0.8-bin.jar
生成代码配置文件generatorConfig.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC"-//mybatis.org//DTD MyBatis GeneratorConfiguration 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.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8"
userId="root"
password="666666">
</jdbcConnection>
<!--<jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521: mybatis"
userId=" mybatis"
password=" mybatis">
</jdbcConnection>-->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals"value="false" />
</javaTypeResolver>
<!--targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.mybatis.user.model"
targetProject=".\src">
<!--enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages"value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings"value="true" />
</javaModelGenerator>
<!--targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.mybatis.user.mapper"
targetProject=".\src">
<!--enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages"value="false" />
</sqlMapGenerator>
<!--targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.mybatis.user.mapper"
targetProject=".\src">
<!--enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages"value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<!--<table tableName="cy_mybatis"></table>
<tabletableName="cy_mybatis"></table>
<tabletableName="cy_mybatis"></table>
<tabletableName="cy_mybatis"></table>
<tabletableName="cy_mybatis"></table>-->
<table tableName="cy_user"></table>
</context>
</generatorConfiguration>
执行生成程序
public static void main(String[] args) throwsInvalidConfigurationException {
List<String>warnings = newArrayList<String>();
boolean overwrite = true;
//指定逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParsercp = new ConfigurationParser(warnings);
try {
Configurationconfig = cp.parseConfiguration(configFile);
DefaultShellCallbackcallback = newDefaultShellCallback(overwrite);
MyBatisGeneratormyBatisGenerator = newMyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
} catch (IOException |XMLParserException e) {
// TODOAuto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODOAuto-generated catch block
e.printStackTrace();
} catch(InterruptedException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}