草丛三剑客之逆向生成
maven jar依赖
<!–逆向依赖–>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
datasource.properties 配置
db.driverLocation =E:/IDEA/workspace/jd_shop/web/src/main/tools/mysql-connector-java-5.1.6-bin.jar
db.url=jdbc:mysql://localhost:3306/lan_mmal
db.className=com.mysql.jdbc.Driver
db.username=root
db.password=root
generatorConfig.xml(resources文件下)
<?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=”datasource.properties”/>
<!–指定数据库驱动jar包位置–>
<classPathEntry location=”${db.driverLocation}”></classPathEntry>
<context id=”default” targetRuntime=”MyBatis3″>
<commentGenerator>
<!– 是否去除自动生成的注释 true:是 : false:否 –>
<property name=”suppressAllComments” value=”true” />
</commentGenerator>
<!–数据库连接的信息:驱动类、连接地址、用户名、密码 –>
<jdbcConnection driverClass=”${db.className}”
connectionURL=”${db.url}” userId=”${db.username}”
password=”${db.password}”>
</jdbcConnection>
<!– <jdbcConnection driverClass=”oracle.jdbc.OracleDriver”
connectionURL=”jdbc:oracle:thin:@127.0.0.1:1521:yycg”
userId=”yycg”
password=”yycg”>
</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.mall.jd.pojo”
targetProject=”E:\IDEA\workspace\jd_shop\web\src\main\java”>
<!– enableSubPackages:是否让schema作为包的后缀 –>
<property name=”enableSubPackages” value=”false” />
<!– 从数据库返回的值被清理前后的空格 –>
<property name=”trimStrings” value=”true” />
</javaModelGenerator>
<!– targetProject:mapper映射文件生成的位置 –>
<sqlMapGenerator targetPackage=”mapper”
targetProject=”E:\IDEA\workspace\jd_shop\web\src\main\resources”>
<!– enableSubPackages:是否让schema作为包的后缀 –>
<property name=”enableSubPackages” value=”false” />
</sqlMapGenerator>
<!– targetPackage:mapper接口生成的位置 –>
<javaClientGenerator type=”XMLMAPPER”
targetPackage=”com.mall.jd.dao”
targetProject=”E:\IDEA\workspace\jd_shop\web\src\main\java”>
<!– enableSubPackages:是否让schema作为包的后缀 –>
<property name=”enableSubPackages” value=”false” />
</javaClientGenerator>
<!– 指定数据库表 –>
<table tableName=”mmall_cart”></table>
<table tableName=”mmall_category”></table>
<table tableName=”mmall_order”></table>
<table tableName=”mmall_order_item”></table>
<table tableName=”mmall_pay_info”></table>
<table tableName=”mmall_product”></table>
<table tableName=”mmall_shipping”></table>
<table tableName=”mmall_user”></table>
<!–<table schema=”” tableName=”sys_user”></table>
<table schema=”” tableName=”sys_role”></table>
<table schema=”” tableName=”sys_permission”></table>
<table schema=”” tableName=”sys_user_role”></table>
<table schema=”” tableName=”sys_role_permission”></table>–>
<!– 有些表的字段需要指定java类型
<table schema=”” tableName=””>
<columnOverride column=”” javaType=”” />
</table> –>
</context>
</generatorConfiguration>
逆向工具启动类
package com.mall.jd.utils;
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.springframework.util.ResourceUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 逆向工程启动类
*/
public class GeneratorDisplay {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
// File configFile = new File(“E:/IDEA/workspace/jd_shop/web/src/main/resources/generatorConfig.xml”);
File configFile = ResourceUtils.getFile(“classpath:generatorConfig.xml”);
// 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);
}
public static void main(String[] args) throws Exception {
try {
GeneratorDisplay generatorDisplay=new GeneratorDisplay();
generatorDisplay.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}