- 首先明确mybatis逆向工程是要干嘛的?
当我们设计好表后,可以通过mybatisGeneratorCustom逆向工程自动生成pojo类,mapper文件.我们主要需要自动生成pojo类,这样可以省下不少建pojo类的时间.mapper文件还是需要自己根据业务来编写的.
源码github下载地址:https://github.com/wcyong/mybatisGeneratorCustom.git.
源码下载地址中有mybatisGeneratorCustom的介绍.mybatis逆向工程使用非常简单,下面将介绍使用步骤.
- 1.导工程,将mybatisGeneratorCustom工程导入IDE.
- 2.修改配置文件
主要修改数据库连接,数据库名,pojo类,mapper类生成位置,表名等
<?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>
<!--Mybatis3是mybatis版本-->
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!-- 1.修改数据库连接,数据库名-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
password="123456">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<!-- 2.修改POJO类生成位置,在src下的com.test.mytatis.pojo包下-->
<javaModelGenerator targetPackage="com.test.mybatis.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<!--3.修改映射文件生成位置-->
<sqlMapGenerator targetPackage="com.test.mybatis.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<!--4.修改mapper接口生成位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.test.mybatis.pojo" targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<!--5.添加需要生成的表-->
<table schema="" tableName="test1"></table>
<table schema="" tableName="test2"></table>
<!-- 有些表的字段需要指定java类型 -->
<!-- <table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> -->
</context>
</generatorConfiguration>
- 3.运行提供的main函数
指定generator配置文件
public class GeneratorCustom {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
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 {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 4.最后将生产的文件拷入自己的工程.
- 里边的标签有不懂的参考MyBatis Generator 详解:http://blog.csdn.net/weixin_38009266/article/details/77865230
- 转载自:https://blog.csdn.net/weixin_38009266/article/details/77865252