Mybatis逆向工程简介
mybatis是目前很流行的持久层框架,其逆向工程更是大大缩减了开发时间。所谓mybatis逆向工程,就是mybatis会根据设计好的数据表,自动生成pojo、mapper以及mapper.xml。
一.导入对应的jar包
如果使用的是Maven模型构建的工程,导入对应的jar包只需要在pox.xml文件中<dependencies>标签内添加如下代码:
<!-- Mybaits逆转工程依赖包 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
如果使用导入jar包的方式,可以去GitHub上下载:页面链接
二.创建generatorConfig.xml文件
在项目文件中创建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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- Mysql数据库连接的信息:驱动类、连接地址、用户名、密码 ,根据本机数据库修改-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root"
password="123456">
</jdbcConnection>
<!-- Oracle数据库
<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:生成POJO类的位置 -->
<javaModelGenerator targetPackage="com.test.pojo" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.test.mapper" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetProject:mapper接口生成的的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据表 ,如果需要添加新的数据表,根据下面代码格式进行添加即可-->
<table schema="" tableName="customer_inf"></table>
<table schema="" tableName="customer_login"></table>
<!-- 有些表的字段需要指定java类型
<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> -->
</context>
</generatorConfiguration>
targetPackage="com.test.dao"
解释:生成对应的文件存放目录(包)。
targetProject=".\src\main\java"
解释:对应目录(包)的路径。
注意事项:
(1).数据库驱动根据导入的jar包进行选择
driverClass="com.mysql.cj.jdbc.Driver"
或者
driverClass="com.mysql.jdbc.Driver"
否则会产生如下错误:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
(2).数据库时区问题
该错误解决方法可以参考另一篇博客:Mybatis创建逆向工程部分报错及处理方式
(3).该配置文件中的
targetPackage="com.test.pojo"
targetPackage="com.test.mapper"
targetPackage="com.test.dao"
这几个文件目录(包)需要提前手动创建。
三.创建GeneratorSqlmap.java文件
文件内容:
package com.test.job;
import java.io.File;
import java.util.*;
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;
public class GeneratorSqlmap {
public void generator() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定配置文件,填写所创建的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);
}
// 执行main方法以生成代码
public static void main(String[] args) {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
本项目文件结构如下:
运行前:
com.test.pojo
com.test.mapper
com.test.dao
以上三个包中无文件。
运行结果:
生成了对应的类,接口及映射文件。
项目代码:
链接:https://pan.baidu.com/s/1GBqkH_v4fjKzV49YWmqtVw
提取码:r4md