Mybatis_generator逆向生成工具
作用
- 生成
pojo
实体类 - 生成
XXXmapper.java
- 生成
XXXmapper.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="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 通用mapper所在目录 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.icoding.my.mapper.MyMapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mall-dev"
userId="root"
password="root">
</jdbcConnection>
<!-- 对应生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.icoding.pojo" targetProject="src/main/java"/>
<!-- 对应生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.icoding.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
<!-- 数据库表 -->
<table tableName="carousel"></table>
<table tableName="category"></table>
<table tableName="items"></table>
<table tableName="items_comments"></table>
<table tableName="items_img"></table>
<table tableName="items_param"></table>
<table tableName="items_spec"></table>
<table tableName="order_items"></table>
<table tableName="order_status"></table>
<table tableName="orders"></table>
<table tableName="user_address"></table>
<table tableName="users"></table>
</context>
</generatorConfiguration>
逆向生成代码工具类
public class GeneratorDisplay {
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 {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
引用项目配置
在pom中引入通用mapper工具
<!-- 通用mapper 逆向工具 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
在application.yml
中引入通用mapper配置
#############################################################
#
# mybatis mapper 配置
#
#############################################################
# 通用mapper配置
mapper:
mappers: com.icoding.my.mapper.MyMapper
not-empty: false # 在进行数据库操作的时候,判断表达式 username != null, 在sql后追加 username != ''
identity: MYSQL
引入MyMapper
接口类(通用数据库操作方法CRUD)
/**
* 继承自己的MyMapper
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
首先看一下MyMapper
所继承的父类,如:
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T>
这里有两个父类,Mapper<T>
, MySqlMapper<T>
,分别看下源码
MySqlMapper
public interface MySqlMapper<T> extends
InsertListMapper<T>,
InsertUseGeneratedKeysMapper<T>
这个类又继承了两个mapper,从类名可以看出是用于数据库插入操作的,这两个类又分别包含了如下方法:
方法名 | 操作 | 备注 |
---|---|---|
insertList(list) | 数据库批量插入 | 主键需自增 |
InsertUseGeneratedKeysMapper(record) | 插入表数据 | 主键需自增 |
在传统的JavaWeb开发中,这两个方法是没有问题的,但是在分布式情况下,需要设计全局唯一的分布式主键,此时这两种方法就不能用了。
Mapper
public interface Mapper<T> extends
BaseMapper<T>,
ExampleMapper<T>,
RowBoundsMapper<T>,
Marker
分别来看一下各父类中的方法有些啥?
- BaseMapper
-
ExampleMapper
Example
类是用于提供给用户实现自定义条件的,也就是where
条件的
-
RowBoundsMapper
用于分页.