0、MybatisPlus
简介:是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生,支持多种数据库
官方文档:https://baomidou.com/pages/24112f/
特性
1、快速入门
1、搭建环境
- IDE:IntelliJ IDEA 2020.2.3
- JDK:JDK 8+
- 构建工具:maven 3.6.3
- MySQL版本:MySQL 8.0
- Spring Boot:2.6.6
- MyBatis-Plus:3.3.2
2、创建数据库及表
创建表
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
插入数据
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
3、创建Springboot项目
4、导入依赖
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--springboot-test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
5、编写并链接数据库配置
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456789
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6、编写实体类以及对应的接口实现
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
UserMapper.java
package com.lsa.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lsa.pojo.User;
import org.springframework.stereotype.Repository;
@Repository//表示持久层
public interface UserMapper extends BaseMapper<User> {
}
7、测试
@SpringBootTest
@RunWith(SpringRunner.class)
class MybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
/*查询全部*/
List<User> userList = this.userMapper.selectList(null);
userList.forEach(System.out::println);
}
2、CRUD扩展
1、insert
/*1.插入*/
@Test
void insert() {
User user = new User();
user.setName("lsa");
user.setAge(22);
user.setEmail("1213756681@qq.com");
int insert = this.userMapper.insert(user);
System.out.println("插入的数据:" + insert);
}
数据库插入的id默认值为:全局的唯一id
主键生成策略
1.源码分析
public enum IdType {
AUTO(0),//数据库id自增
NONE(1), //该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
INPUT(2),//手动输入
ASSIGN_ID(3), //雪花算法默认全局id
ASSIGN_UUID(4),//分配 UUID
/** @deprecated */
@Deprecated
ID_WORKER(3),//mp自带策略,会生成19位的值,需要的注意的是这个只能放在数字类型的主键中。
/** @deprecated */
@Deprecated
ID_WORKER_STR(3),//字符串全局唯一ID (idWorker 的字符串表示)。同ID_WORKER不同的是它是只能放在String类型的主键中。
/** @deprecated */
@Deprecated
UUID(4);//全局唯一id(uuid)
}
@Getter
public enum IdType {
/**
* 数据库ID自增
*/
AUTO(0),
/**
* 该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
*/
NONE(1),
/**
* 用户输入ID
* <p>该类型可以通过自己注册自动填充插件进行填充</p>
*/
INPUT(2),
/* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
/**
* 分配ID (主键类型为number或string),
* 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(雪花算法)
*/
ASSIGN_ID(3),
/**
* 分配UUID (主键类型为 string)
* 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(UUID.replace("-",""))
*/
ASSIGN_UUID(4),
/**
* @deprecated 3.3.0 please use {@link #ASSIGN_ID}
*/
@Deprecated
ID_WORKER(3),
/**
* @deprecated 3.3.0 please use {@link #ASSIGN_ID}
*/
@Deprecated
ID_WORKER_STR(3),
/**
* @deprecated 3.3.0 please use {@link #ASSIGN_UUID}
*/
@Deprecated
UUID(4);
}
默认 : ASSIGN_ID(3) 雪花算法默认全局唯一id
3.3.0 开始,默认使用雪花算法+UUID
分布式系统唯一Id生成:https://www.cnblogs.com/haoxinyue/p/5208136.html
雪花算法:
snowflake是Twitter开源的分布式ID生成算法,结果是一个Long型的ID。其核心思想是:使用41bit作为毫秒数,10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID),12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID),最后还有一个符号位,永远是0。
主键自增
我们需要配置主键自增:
1、实体类字段上:@TableId(value = “id”, type = IdType.AUTO)
2、数据库字段一定要是自增不然报错(必须勾选)
3、再次测试插入自增+1
2、更新操作
代码:
@Test//测试更新
public void updateTest() {
User user = new User();
user.setId(5L);//输入需要修改数据的id
//通过条件自动拼接动态Sql
user.setName("zck");
user.setAge(5);
user.setEmail("r1ot@qq.com");
int i = userMapper.updateById(user);//updateById,但是参数是个user
System.out.println(i);
}
你修改了哪里mybatis-plus会帮你自动更新
3、自动填充(时间)
创建时间、更改时间! 这些操作一般都是自动化完成,我们不希望手动更新
阿里巴巴开发手册︰几乎所有的表都要配置 gmt_create、gmt_modified !而且需要自动化
方式一:数据库级别(工作中不允许修改数据库级别)
1、在表中增加字段:create_time,update_time
2、新增字段
private Date createTime;
private Date updateTime;
再次进行修改,发现时间已经更新了而我们没有去操作,自动帮我们填充了
这种就是数据库级别的操作
方式二:代码级别
1、删除数据库的默认值,更新操作!
2、实体类字段属性上需要增加注解
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
3、自定义实现类 MyMetaObjectHandler
package com.lsa.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
//插入时填策略
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill ....");
//setFieldValByName(java.lang.String fieldName, java.lang.Object fieldVal, org.apache.ibatis.reflection.MetaObject metaObject)
this.strictInsertFill(metaObject, "createTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
// this.setFieldValByName("createTime", new Date(), metaObject);
// this.setFieldValByName("updateTime", new Date(), metaObject);
}
//更新时填策略
@Override
public void updateFill(MetaObject metaObject) {
log.info("==start update ······==");
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐)
// this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
如果插入和更新时间为null的话记得实体类Date要用Util下的时间
4、测试结果
@Test//测试更新
public void updateTest() {
User user = new User();
user.setId(1640657434972143622L);//输入需要修改数据的id
//通过条件自动拼接动态Sql
user.setName("zck");
user.setAge(3);
user.setEmail("r1ot@qq.com");
int i = userMapper.updateById(user);//updateById,但是参数是个user
System.out.println(i);
}
@Test//测试插入
public void insertTest() {
User user = new User();
user.setName("lsalsalovezck");
user.setAge(23);
user.setEmail("1213756681@qq.com");
Integer result = userMapper.insert(user); //会帮我们自动生成id
System.out.println(result); //受影响的行数
System.out.println(user); //通过日志发现id会自动回填
}
5、源码解释
public enum FieldFill {
/**
* 默认不处理
*/
DEFAULT,
/**
* 插入填充字段
*/
INSERT,
/**
* 更新填充字段
*/
UPDATE,
/**
* 插入和更新填充字段
*/
INSERT_UPDATE
}
3、乐观锁&悲观锁
乐观锁:顾名思义就是十分乐观,他总是认为自己不会出现问题,无论干什么都不上锁,如果出现了问题再去更新值测试
悲观锁:顾名思义就是十分悲观,他总是认为自己会出现问题,无论干什么都会上锁,再去操作
1、乐观锁的讲解
乐观锁的实现方式:
- 取出记录时,获取当前version
- 更新时,带上这个version
- 执行跟新时,config帮我们做 set version = newVsrsion(version+1操作) where version = oldVersion(前边查出来的version)
- 如果version不对,就更新失败
乐观锁:1、先查询,获取版本号 version = 1
---线程1
update user set name = "lsa" where id = 1 and version = version+1
---线程2 线程2在线程1没有更新前抢先完成了,这个时候version=2,线程1更新失败
update user set name = "lsa" where id = 1 and version = version+1
插队就是,在线程1没有执行更新之前设置了线程2并进行更新,在更新线程1,如果没有乐观锁他就会覆盖插队线程的值
测试一下Mybatis-Plus乐观锁插件
1、给数据库中增加version字段
2、实体类加对应的字段
@Version
private Integer version;
3、注册乐观锁插件
@MapperScan("com.lsa.mapper")
@Configuration
public class MybatisplusConfig {
// 注册乐观锁插件 3.3版本
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
//注册乐观锁插件 版本3.5
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//乐观锁
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
4、测试
@Test//测试更新
public void updateTest() {
//1.查询用户信息
User user = this.userMapper.selectById(1640657434972143622L);
//2.修改用户信息
user.setId(1640657434972143622L);//输入需要修改数据的id
//通过条件自动拼接动态Sql
user.setName("lsalovezck111");
user.setAge(5);
user.setEmail("1213756681@qq.com");
int i = userMapper.updateById(user);//updateById,但是参数是个user
System.out.println(i);
}
线程插队测试:
@Test//测试更新
public void updateTest() {
//线程1
User user1 = this.userMapper.selectById(1);
user1.setName("lsalovezck111");
user1.setEmail("1213756681@qq.com");
//线程2
User user2 = this.userMapper.selectById(1);
user2.setName("lsalovezck222");
user2.setEmail("1213756681@qq.com");
userMapper.updateById(user2);//updateById,但是参数是个user
userMapper.updateById(user1);//updateById,但是参数是个user
}
如果不用乐观锁的话,线程2更新完后会被线程1覆盖
上诉的意思就是执行更新的时候,线程2先进行更新,线程2更新的时候会多一个判断version是否等于线程2的,然后进行version+1
乐观锁:
就是更新的时候
1.先获取version
2.更新时,带上这个version
3.执行跟新时,会执行version =(newversion)oldversion+1 where version = oldversion
说明:
- 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
- 整数类型下
newVersion = oldVersion + 1
newVersion
会回写到entity
中- 仅支持
updateById(id)
与update(entity, wrapper)
方法 - 在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
4、Select
1、单条插叙
@Test//查询测试
public void selectTest() {
User user = this.userMapper.selectById(1);
System.out.println(user);
}
2、多个查询
@Test//查询测试
public void selectTest() {
List<Integer> idList = new ArrayList<>();
idList.add(1);
idList.add(2);
idList.add(3);
List<User> userList = this.userMapper.selectBatchIds(idList);
userList.forEach(System.out::println);
}
3、map查询
@Test//查询测试
public void selectTest() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "lsa");
this.userMapper.selectByMap(map);
}
5、分页查询
1、注册组件
//注册分页插件 3.3.*版本
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
2、测试
@Test//查询测试
public void selectPage() {
Page<User> userPage = new Page<>(1, 3);
Page<User> selectPage = this.userMapper.selectPage(userPage, null);
selectPage.getRecords().forEach(System.out::println);
System.out.println("获取查询的总数:" + selectPage.getTotal());
}
6、Delete
1、单条删除
@Test//查询测试
public void deleteTest() {
this.userMapper.deleteById(1640657434972143618L);
}
2、多条删除
@Test//查询测试
public void deleteTest() {
List<Long> list = new ArrayList<>();
list.add(1640657434972143619L);
list.add(1640657434972143620L);
this.userMapper.deleteBatchIds(list);
}
3、条件删除
@Test//查询测试
public void deleteTest() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "lsalsa");
map.put("age", "21");
this.userMapper.selectByMap(map);
}
7、逻辑删除
物理删除:从数据库中直接删除
逻辑删除:在数据库中没有被删除,而是通过一个变量来使他失效! deleted=0 ==> deleted=1
管理员可以查看被删除的记录!防止数据的丢失,类似于回收站!
1、在数据库中添加deleted字段
2、添加字段
@TableLogic//逻辑删除注解
private Integer deleted;
3、配置参数
#配置逻辑删除 没删除的为0 删除的为1
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
4、测试一下删除
@Test//查询测试
public void deleteTest() {
int deleteById = this.userMapper.deleteById(1640657434972143624L);
System.out.println(deleteById);
}
8、性能插件分析
因为3.2.0以后的版本就没有了,所有使用p6spy
//性能分析插件@Bean@Profile({"dev","test"})//设置dev开发、test测试 环境开启 保证我们的效率public PerformanceInterceptor performanceInterceptor(){ PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); performanceInterceptor.setMaxTime(100);//设置sql最大执行时间*ms,如果超过了则不执行 performanceInterceptor.setFormat(true);//开启sql格式化 return performanceInterceptor;}
1、导入依赖
<!--性能你分析插件-->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.8.7</version>
</dependency>
2、导入 spy.properties 文件
#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=1
3、修改链接配置
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://127.0.0.1:3306/mybatis_plus?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456789
type: com.alibaba.druid.pool.DruidDataSource
9、条件查询器Wrapper
1、非空测试
@Test
public void testWrapper1() {
//参数是一个wrapper ,条件构造器,和刚才的map对比学习!
//查询name不为空,email不为空,age大于18的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.isNotNull("name")
.isNotNull("email")
.ge("age", 18);
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
2、单条查询
@Test
public void testWrapper1() {
//参数是一个wrapper ,条件构造器,和刚才的map对比学习!
//查询name不为空,email不为空,age大于18的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.isNotNull("name")
.isNotNull("email")
.ge("age", 18);
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
3、区间查询
@Test
public void testWrapper3() {
//查询age在10-20之间的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age", 10, 20);//区间
Long count = userMapper.selectCount(wrapper);//输出查询的数量selectCount
System.out.println(count);
}
4、模糊查询
@Test
public void testWrapper4() {
//模糊查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.notLike("name","s")//名字不带s的
.likeRight("email","t");//右边模糊查询 t%
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
maps.forEach(System.out::println);
}
5、子查询
@Test
public void testWrapper5() {
//模糊查询
// SELECT id,name,age,email,version,deleted,create_time,update_time
//FROM user
//WHERE deleted=0 AND id IN
//(select id from user where id<5)
QueryWrapper<User> wrapper = new QueryWrapper<>();
//id 在子查询中查出来
wrapper.inSql("id","select id from user where id<5");
List<Object> objects = userMapper.selectObjs(wrapper);
objects.forEach(System.out::println);
}
6、降序查询
@Test
public void testWrapper6() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//通过id进行降序排序
wrapper.orderByDesc("id");
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
3、代码自动生成器
AutoGenerator
是 MyBatis-Plus 的代码生成器,通过 AutoGenerator
可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
package com.lsa;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
//创建对象
AutoGenerator autoGenerator = new AutoGenerator();
//1.数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatis_plus");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("123456789");
autoGenerator.setDataSource(dataSourceConfig);
//2.全局配置
GlobalConfig globalConfig = new GlobalConfig();
/*配置项目路径*/
globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
globalConfig.setAuthor("lsa");
/*是否打开资源管理器*/
globalConfig.setOpen(false);
/*是否覆盖之前生成的*/
globalConfig.setFileOverride(false);
//去掉Service的I前缀
globalConfig.setServiceName("%sService");
/*配置swagger 看情况加*/
// globalConfig.setSwagger2(true);
autoGenerator.setGlobalConfig(globalConfig);
//3.包配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.lsa");
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setController("controller");
packageConfig.setModuleName(null);
autoGenerator.setPackageInfo(packageConfig);
//4.策略配置
StrategyConfig strategyConfig = new StrategyConfig();
//设置要映射的表明
strategyConfig.setInclude("user");
//包的命名规则下划线转驼峰
// 数据库表映射到实体的命名策略:下划线转驼峰
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
// 数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
//自动lombok
strategyConfig.setEntityLombokModel(true);
//逻辑删除字段 没有课不写
strategyConfig.setLogicDeleteFieldName("deleted");
//自动填充配置时间 没有可不写
TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
TableFill gmtUpdate = new TableFill("update_time", FieldFill.INSERT_UPDATE);
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(gmtCreate);
tableFillList.add(gmtUpdate);
strategyConfig.setTableFillList(tableFillList);
//乐观锁配置
strategyConfig.setVersionFieldName("version");
strategyConfig.setRestControllerStyle(true);//开启驼峰命名
strategyConfig.setControllerMappingHyphenStyle(true);
autoGenerator.setStrategy(strategyConfig);
//启动
autoGenerator.execute();
}
}
// 数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
//自动lombok
strategyConfig.setEntityLombokModel(true);
//逻辑删除字段 没有课不写
strategyConfig.setLogicDeleteFieldName("deleted");
//自动填充配置时间 没有可不写
TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
TableFill gmtUpdate = new TableFill("update_time", FieldFill.INSERT_UPDATE);
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(gmtCreate);
tableFillList.add(gmtUpdate);
strategyConfig.setTableFillList(tableFillList);
//乐观锁配置
strategyConfig.setVersionFieldName("version");
strategyConfig.setRestControllerStyle(true);//开启驼峰命名
strategyConfig.setControllerMappingHyphenStyle(true);
autoGenerator.setStrategy(strategyConfig);
//启动
autoGenerator.execute();
}
}
运行即可生成,具体的小细节看配置了