
MyBatis Plus
简介官网:http://mp.baomidou.com/
参考教程:http://mp.baomidou.com/guide/
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特性
- 无侵入: 只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小: 启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象 *** 作
- 强大的 CRUD *** 作: 内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD *** 作,更有强大的条件构造器,满足各类使用需求
- 支持 Lambda 形式调用: 通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
- 支持多种数据库: 支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库
- 支持主键自动生成: 支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
- 支持 XML 热加载: Mapper 对应的 XML 支持热加载,对于简单的 CRUD *** 作,甚至可以无 XML 启动
- 支持 ActiveRecord 模式: 支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD *** 作
- 支持自定义全局通用 *** 作: 支持全局通用方法注入( Write once, use anywhere )
- 支持关键词自动转义: 支持数据库关键词(order、key......)自动转义,还可自定义关键词
- 内置代码生成器: 采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
- 内置分页插件: 基于 MyBatis 物理分页,开发者无需关心具体 *** 作,配置好插件之后,写分页等同于普通 List 查询
- 内置性能分析插件: 可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
- 内置全局拦截插件: 提供全表 delete 、 update *** 作智能分析阻断,也可自定义拦截规则,预防误 *** 作
- 内置 Sql 注入剥离器: 支持 Sql 注入剥离,有效预防 Sql 注入攻击
首先创建测试数据库
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) ); INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@athome.com'), (2, 'Jack', 20, 'test2@athome.com'), (3, 'Tom', 28, 'test3@athome.com'), (4, 'Sandy', 21, 'test4@athome.com'), (5, 'Billie', 24, 'test5@athome.com');
项目相关配置
- pom.xml
然后搭建项目基础环境,使用Spring Initializer快速初始化一个 Spring Boot 工程,然后引入相关依赖
com.baomidou mybatis-plus-boot-starter3.2.0
- application.properties
# mysql spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc://localhost:3306/mybatisplus?characterEncoding=UTF-8 #mybatis日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
- 启动类
增加包扫描
@SpringBootApplication
@MapperScan("com.athome.mp.mapper")
public class MpDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MpDemoApplication.class, args);
}
}
- 编写实体类
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
- mapper
public interface UserMapper extends baseMapper{ }
- 测试启动
@RunWith(SpringRunner.class)
@SpringBootTest
public class MpDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
List users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
- 注意
一般来说,自此就可以成功查询到数据库消息,但是也可能会有意外发生。
在application.properties配置文件中添加 MySQL 数据库的相关配置:
mysql 5
#mysql数据库连接 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?useSSL=false spring.datasource.username=root spring.datasource.password=root
mysql8以上(spring boot 2.1)
注意:driver和url的变化
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root
注意:
- 这里的 url 使用了 ?serverTimezone=GMT%2B8 后缀,因为Spring Boot 2.1 集成了 8.0版本的jdbc驱动,这个版本的 jdbc 驱动需要添加这个后缀,否则运行测试用例报告如下错误:
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more
- 这里的 driver-class-name 使用了 com.mysql.cj.jdbc.Driver ,在 jdbc 8 中 建议使用这个驱动,之前的 com.mysql.jdbc.Driver 已经被废弃,否则运行测试用例的时候会有 WARN 信息
- 小结
通过以上几个简单的步骤,我们就实现了 User 表的 CRUD 功能,甚至连 XML 文件都不用编写!
从以上步骤中,我们可以看到集成MyBatis-Plus非常的简单,只需要引入 starter 工程,并配置 mapper 扫描路径即可。
常用注解官方文档:https://mp.baomidou.com/guide/annotation.html#tablename
CRUD insert插入
@Test
public void testInsert(){
User user = new User();
user.setAge(18);
user.setEmail("163@163.com");
user.setName("AAA");
int result = userMapper.insert(user);
System.out.println(result); //返回受影响的行数
System.out.println(user); //id自动回填
}
- 注意:数据库插入id值默认为:全局唯一id
-
ID_WORKER MyBatis-Plus默认的主键策略是:ID_WORKER 全局唯一ID
-
自增策略
- 要想主键自增需要配置如下主键策略
- 需要在创建数据表的时候设置主键自增
- 实体字段中配置 @TableId(type = IdType.AUTO)
- 要想主键自增需要配置如下主键策略
@TableId(type = IdType.AUTO) private Long id;
要想影响所有实体的配置,可以设置全局主键配置
#全局设置主键生成策略 mybatis-plus.global-config.db-config.id-type=auto
其它主键策略:分析 IdType 源码可知
@Getter
public enum IdType {
AUTO(0),
NONE(1),
INPUT(2),
ID_WORKER(3),
UUID(4),
ID_WORKER_STR(5);
private final int key;
IdType(int key) {
this.key = key;
}
}
Update
根据id修改
@Test
public void testUpdateById(){
User user = new User();
user.setId(1167050374682607617L);
user.setName("BBB");
int result = userMapper.updateById(user);
System.out.println(result); //返回受影响的行数
}
update时生成的sql自动是动态sql:UPDATE user SET name=? WHERe id=?
自动填充项目中经常会遇到一些数据,每次都使用相同的方式填充,例如记录的创建时间,更新时间等。
我们可以使用MyBatis Plus的自动填充功能,完成这些字段的赋值工作:
数据库表中添加自动填充字段
在User表中添加datetime类型的新的字段 create_time、update_time
自动填充方案一、数据级别的自动填充,分别设置默认值:
ALTER TABLE `user` ADD COLUMN `create_time` timestamp ALTER TABLE `user` ADD COLUMN `update_time` timestamp
自动填充方案二、使用MyBatis Plus内置的方法:
- 实现元对象处理器接口:com.baomidou.mybatisplus.core.handlers.metaObjectHandler
- 注解填充字段 @TableField(.. fill = FieldFill.INSERT) 生成器策略部分也可以配置!
- User 新增两个属性
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
- 自定义实现类 UserHandler
@Component
public class UserHandler implements metaObjectHandler {
@Override
public void insertFill(metaObject metaObject) {
//当新增时则会进入到此方法
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
}
@Override
public void updateFill(metaObject metaObject) {
//当修改时则会进入到此方法
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
- 可以启动测试类中的新增和修改方法查看效果。
- 主要适用场景 当要更新一条记录的时候,希望这条记录没有被别人更新
乐观锁实现方式:
- 取出记录时,获取当前version
- 更新时,带上这个version
- 执行更新时, set version = newVersion where version = oldVersion
- 如果version不对,就更新失败
乐观锁配置需要2步 记得两步
1.1 配置插件
数据库添加字段version
ALTER TABLE `user` ADD COLUMN `version` INT DEFAULT 0
新建一个配置类
@Configuration
@MapperScan("com.athome.mp.mapper")
public class MyBatisPlusConfig {
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
}
2.注解实体字段 @Version 必须要!
@Version private Integer version;
- 测试
@Test
public void testVersion(){
// 这里查出来的version相等 version都等于0
User user = userMapper.selectById(1);
User user1 = userMapper.selectById(1);
// UPDATE user SET name=?, update_time=?, version=?, email=?, age=? WHERe id=? AND version=?
// 因为version版本和数据库相等 所以可以修改成功,修改成功后数据库version值+1
user.setName("Version1");
userMapper.updateById(user);
// sql语句相同,但是数据库已经没有version值为零,并且id为1的记录 所以修改失败
user1.setName("Version2");
userMapper.updateById(user1);
}
4.结果展示
4.1 sql语句
4.2 数据库
特别说明:
- 支持的数据类型只有 int,Integer,long,Long,Date,Timestamp,LocalDateTime
- 整数类型下 newVersion = oldVersion + 1
- newVersion 会回写到 entity 中
- 仅支持 updateById(id) 与 update(entity, wrapper) 方法
- 在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
根据id查询
@Test
public void testSelectById(){
User user = userMapper.selectById(1);
System.out.println(user);
}
根据多个id查询
@Test
public void testSelectByIds(){
// selectBatchIds 需要的参数是Collection类型,所以还可以这样
List users = userMapper.selectBatchIds(Arrays.asList(1L,2L));
users.forEach(System.err :: println);
}
简单的条件查询
@Test
public void testSelectByMap() {
Map map = new HashMap<>();
map.put("name", "BBB");
// key对应的是数据库的字段名,而不是user类中的属性名
map.put("create_time", null);
List users = userMapper.selectByMap(map);
users.forEach(System.err::println);
}
分页查询
MyBatis Plus 自带分页插件,所以只需要配置即可
- 在配置类MyBatisPlusConfig中配置分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
// paginationInterceptor.setLimit(你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制);
return new PaginationInterceptor();
}
- 测试selectPage分页测试: 最终通过page对象获取相关数据
@Test
public void testSelectPage() {
Page page = new Page<>(2, 5);
userMapper.selectPage(page, null);
//遍历查询结果
// SELECT id,create_time,name,update_time,version,email,age FROM user LIMIT 5,5
page.getRecords().forEach(System.err::println);
System.out.println("当前页:" + page.getCurrent());
System.out.println("总页数:" + page.getPages());
System.out.println("总条数" + page.getTotal());
System.out.println("页面显示条数" + page.getSize());
System.out.println("是否有下一页:" + page.hasNext());
System.out.println("是否有上一页:" + page.hasPrevious());
}
- 测试selectMapsPage分页,返回结果集是Map
@Test
public void testSelectMapsPage() {
Page page = new Page<>(2, 5);
IPage
Delete
根据id删除
@Test
public void testDeleteById(){
int result = userMapper.deleteById(8L);
System.out.println(result);
}
批量删除
@Test
public void testDeleteBatchIds() {
int result = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
System.out.println(result);
}
简单的条件删除
@Test
public void testDeleteByMap() {
HashMap map = new HashMap<>();
map.put("name", "Helen");
map.put("age", 18);
int result = userMapper.deleteByMap(map);
System.out.println(result);
}
逻辑删除
- 物理删除:真实删除,将对应数据从数据库中删除,之后查询不到此条被删除数据
- 逻辑删除:假删除,将对应数据中代表是否被删除字段状态修改为“被删除状态”,之后在数据库中仍旧能看到此条数据记录
- 数据库中添加 is_delete字段
ALTER TABLE `user` ADD COLUMN `is_delete` boolean DEFAULT 0
- 添加类属性 并加上 @TableLogic 注解
这里就不需要使用@TableField(fill = FieldFill.INSERT)注解自动填充,因为数据库里默认为0
@TableLogic private Integer isDelete;
- application.properties 加入配置
此为默认值,如果你的默认值和mp默认的一样,该配置可无
mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0
- 在 MybatisPlusConfig 中注册 Bean (MP 3.1.1开始不再需要这一步):
@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
- 启动删除代码测试
- 测试后发现,数据并没有被删除,deleted字段的值由0变成了1
- 测试后分析打印的sql语句,是一条updateUPDATE user SET is_delete=1 WHERe id=? AND is_delete=0注意: 被删除数据的deleted 字段的值必须是 0,才能被选取出来执行逻辑删除的 *** 作
@Test
public void testLogicDelete() {
int result = userMapper.deleteById(1L);
System.out.println(result);
}
- 测试逻辑删除后的查询
MyBatis Plus中查询 *** 作也会自动添加逻辑删除字段的判断
执行上面的分页查询方法,查看sql
SELECT id,create_time,is_delete,name,update_time,version,email,age FROM user WHERe is_delete=0 LIMIT ?,?
总结: 效果: 使用mp自带方法删除和查找都会附带逻辑删除功能 (自己写的xml不会)
条件构造器 Wapper- Wrapper : 条件构造抽象类,最顶端父类
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- QueryWrapper : Entity 对象封装 *** 作类,不是用lambda语法
- UpdateWrapper : Update 条件封装,用于Entity对象更新 *** 作
- AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
- LambdaQueryWrapper :看名称也能明白就是用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
注意: 以下条件构造器的方法入参中的 column 均表示数据库字段
ge、gt、le、lt、isNull、isNotNull
@Test
public void testWrapper(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper
.isNull("name")
.ge("age", 12)
.isNotNull("email");
int result = userMapper.delete(queryWrapper);
System.out.println("delete return count = " + result);
}
打印sql:
UPDATE user SET deleted=1 WHERe deleted=0 AND name IS NULL AND age >= ? AND email IS NOT NULL
eq、ne
注意: seletOne返回的是一条实体记录,当出现多条时会报错
@Test
public void testSelectOne() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "Tom");
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
}
SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERe deleted=0 AND name = ?
between、notBetween
包含大小边界
@Test
public void testSelectCount() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.between("age", 20, 30);
Integer count = userMapper.selectCount(queryWrapper);
System.out.println(count);
}
SELECT COUNT(1) FROM user WHERe deleted=0 AND age BETWEEN ? AND ?
allEq
@Test
public void testSelectList() {
QueryWrapper queryWrapper = new QueryWrapper<>();
Map map = new HashMap<>();
map.put("id", 2);
map.put("name", "Jack");
map.put("age", 20);
queryWrapper.allEq(map);
List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
SELECT id,create_time,is_delete,name,update_time,version,email,age FROM user WHERe is_delete=0 AND (name = ? AND id = ? AND age = ?)
like、notLike、likeLeft、likeRight
selectMaps返回Map集合列表
@Test
public void testSelectMaps() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper
.notLike("name", "e")
.likeRight("email", "t");
List
SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERe deleted=0 AND name NOT LIKE ? AND email LIKE ?
in、notIn、inSql、notinSql、exists、notExists
- in、notIn:
- notIn("age",{1,2,3})--->age not in (1,2,3)
- notIn("age", 1, 2, 3)--->age not in (1,2,3)
- inSql、notinSql:可以实现子查询
- 例: inSql("age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)
- 例: inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)
@Test
public void testSelectObjs() {
QueryWrapper queryWrapper = new QueryWrapper<>();
//queryWrapper.in("id", 1, 2, 3);
queryWrapper.inSql("id", "select id from user where id < 3");
List
SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERe deleted=0 AND id IN (select id from user where id < 3)
UpdateWrapperor、and
注意: 这里使用的是 UpdateWrapper
不调用or则默认为使用 and 连
@Test
public void testUpdate1() {
//修改值
User user = new User();
user.setAge(99);
user.setName("Andy");
//修改条件
UpdateWrapper userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper
.like("name", "h")
.or()
.between("age", 20, 30);
int result = userMapper.update(user, userUpdateWrapper);
System.out.println(result);
}
UPDATE user SET name=?, age=?, update_time=? WHERe deleted=0 AND name LIKE ? OR age BETWEEN ? AND ?
嵌套or、嵌套and
这里使用了lambda表达式,or中的表达式最后翻译成sql时会被加上圆括号
@Test
public void testUpdate2() {
//修改值
User user = new User();
user.setAge(99);
user.setName("Andy");
//修改条件
UpdateWrapper userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper
.like("name", "h")
.or(i -> i.eq("name", "李白").ne("age", 20));
int result = userMapper.update(user, userUpdateWrapper);
System.out.println(result);
}
UPDATE user SET name=?, age=?, update_time=? WHERe deleted=0 AND name LIKE ? OR ( name = ? AND age <> ? )
orderBy、orderByDesc、orderByAsc
@Test
public void testSelectListOrderBy() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id");
List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERe deleted=0 ORDER BY id DESC
last
直接拼接到 sql 的最后注意: 只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
@Test
public void testSelectListLast() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.last("limit 1");
List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
SELECT id,name,age,email,create_time,update_time,deleted,version FROM user WHERe deleted=0 limit 1
指定要查询的列
@Test
public void testSelectListColumn() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.select("id", "name", "age");
List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
SELECT id,name,age FROM user WHERe deleted=0
set、setSql
最终的sql会合并 user.setAge(),以及 userUpdateWrapper.set() 和 setSql() 中 的字段
@Test
public void testUpdateSet() {
//修改值
User user = new User();
user.setAge(99);
//修改条件
UpdateWrapper userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper
.like("name", "h")
.set("name", "老李头")//除了可以查询还可以使用set设置修改的字段
.setSql(" email = '123@qq.com'");//可以有子查询
int result = userMapper.update(user, userUpdateWrapper);
}
UPDATE user SET age=?, update_time=?, name=?, email = '123@qq.com' WHERe deleted=0 AND name LIKE ?
代码生成器AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
官方使用教程 : https://mp.baomidou.com/guide/generator.html#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B
使用教程添加 代码生成器 依赖
com.baomidou mybatis-plus-generator3.2.0
添加 模板引擎 依赖
MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,用户可以选择自己熟悉的模板引擎,如果都不满足您的要求,可以采用自定义模板引擎。
org.apache.velocity velocity-engine-core2.1
代码
@Test
public void genCode() {
String moduleName = "mp";
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("萧一旬"); // 作者名
gc.setOpen(false); //生成后是否打开资源管理器
gc.setFileOverride(false); //重新生成时文件是否覆盖
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setIdType(IdType.ID_WORKER_STR); //主键策略
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
gc.setSwagger2(true);//开启Swagger2模式
mpg.setGlobalConfig(gc);
// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/mybatisplus");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(moduleName); //模块名
pc.setParent("com.athome");
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("\w*");//设置要映射的表名 \w* 表示匹配所有表名 你也可以加前缀 例如 tb_\w* 表示映射所有以tb_开头的表
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setTablePrefix(pc.getModuleName() + "_");//设置表前缀不生成
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式 *** 作
strategy.setLogicDeleteFieldName("is_delete");//逻辑删除字段名
strategy.setEntityBooleanColumnRemoveIsPrefix(false);//去掉布尔值的is_前缀
//自动填充
TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
ArrayList tableFills = new ArrayList<>();
tableFills.add(gmtCreate);
tableFills.add(gmtModified);
strategy.setTableFillList(tableFills);
strategy.setVersionFieldName("version");//乐观锁列
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
mpg.setStrategy(strategy);
// 6、执行
mpg.execute();
}
效果图
红框内为生成代码
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)