1、添加 pom 依赖:
<!-- 逆向工程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.4</version>
</dependency>
<!--====================-->
2、添加配置:
<!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.jdbc.Driver"
connectionURL="jdbc:mysql://rm-bp14b4ud3l5yoy3f6po.mysql.rds.aliyuncs.com:3306/zhongshu_prod_db" userId="root"
password="Liyi880301">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.zhongshu.vegetables.bean"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.zhongshu.vegetables.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.zhongshu.vegetables.dao"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="area"></table>
<table schema="" tableName="category"></table>
<table schema="" tableName="common"></table>
<table schema="" tableName="dictionary"></table>
<table schema="" tableName="item"></table>
<table schema="" tableName="menu"></table>
<table schema="" tableName="menu_role"></table>
<table schema="" tableName="user"></table>
<table schema="" tableName="order"></table>
<table schema="" tableName="order_detail"></table>
<table schema="" tableName="receive_address"></table>
<table schema="" tableName="role"></table>
<table schema="" tableName="shop_cart"></table>
<table schema="" tableName="unit"></table>
<table schema="" tableName="version"></table>
</context>
</generatorConfiguration>
3、执行代码启动
package com.zhongshu.vegetables.utils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorSqlmap {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("./src/main/resources/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 {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、脚本方式启动逆向工程
新建文件名 run_mysql.bat
内容: java -jar lib/mybatis-generator-core-1.3.1-ext.jar -configfile generatorConfig_mysql.xml
双击运行即可 !
******************************************************华丽丽滴分界线*************************************
MySQL
只需配置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="D:\lion\LionTest\WebContent\WEB-INF\lib\sqljdbc4.jar" />
<context id="context1">
<!--<commentGenerator>
<!– 去除自动生成的注释 –>
<property name="suppressAllComments" value="true" />
</commentGenerator>-->
<!-- 是否生成注释 去除自动生成的注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接配置 -->
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionURL="jdbc:sqlserver://localhost:1433;DatabaseName=tvshow" userId="sa" password="sa" />
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--配置生成的实体包
targetPackage:生成的实体包位置,默认存放在src目录下
targetProject:目标工程名
-->
<javaModelGenerator targetPackage="com.lion.pojo" targetProject="SqlTest" />
<!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->
<sqlMapGenerator targetPackage="com.lion.mapper" targetProject="SqlTest" />
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lion.dao" targetProject="SqlTest">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<!-- 配置表
schema:不用填写
tableName: 表名
enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
去除自动生成的例子
-->
<table schema="" tableName="production" domainObjectName="Production" enableCountByExample="false" enableSelectByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
</table>
</context>
</generatorConfiguration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
MS SQL server
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>
<properties resource="jdbc.properties"></properties>
<classPathEntry location="${dbconfig.sqlServer.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="${dbconfig.sqlServer.driverClasss}"
connectionURL="${dbconfig.sqlServer.ssmDemo.read.jdbcUrl}"
userId="${dbconfig.sqlServer.username}"
password="${dbconfig.sqlServer.password}" />
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--配置生成的实体包
targetPackage:生成的实体包位置,默认存放在src目录下
targetProject:目标工程名-->
<javaModelGenerator targetPackage="com.lion.pojo"
targetProject="SqlTest" />
<!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->
<sqlMapGenerator targetPackage="com.lion.mapper" targetProject="SqlTest" />
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lion.dao" targetProject="SqlTest">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<!-- 配置表
schema:不用填写
tableName: 表名
enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
去除自动生成的例子
-->
<table schema=""
tableName="title"
domainObjectName="Title"
enableCountByExample="false"
enableSelectByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false"
selectByExampleQueryId="false" >
</table>
</context>
</generatorConfiguration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
2.配置jdbc.properties
dbconfig.sqlServer.driverClasss=com.microsoft.sqlserver.jdbc.SQLServerDriver
dbconfig.sqlServer.ssmDemo.read.jdbcUrl=jdbc:sqlserver://localhost:1433;DatabaseName=tvshow
dbconfig.sqlServer.username=sa
dbconfig.sqlServer.password=111
#定义初始连接数
#定义最大连接数
dbconfig.maxActive=20
#定义最大空闲
dbconfig.maxIdle=20
#定义最小空闲
dbconfig.minIdle=1
#定义最长等待时间
dbconfig.maxWait=60000
#sqljsbc的位置(和sqlserver版本有关)
dbconfig.sqlServer.driverLocation=D:\sqljdbc4.jar