文章目录:
1.MyBatis-Plus注解
2.准备实体类和表结构
3.MyBatis-Plus增删改查
4.MyBatis-Plus逻辑删除
5.MyBatis-Plus乐观锁插件
6.MyBatis-Plus其他
7.MyBatis-Plus代码生成器
8.MyBatis-Plus配置类
一、配置
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
二、实体类注解
@Version
private Integer version;
三、测试
@Test
public void testVersion() {
// userMapper.insert(new User().setName("小明").setAge(23).setEmail("xm@163.com").setVersion(1));
/*
Preparing: INSERT INTO user ( ID, NAME, AGE, EMAIL, VERSION, CREATE_TIME ) VALUES ( ?, ?, ?, ?, ?, ? )
Parameters: 1213659384740179970(Long), 小明(String), 23(Integer), xm@163.com(String), 1(Integer), 2020-01-05 11:12:00.682(Timestamp)
*/
// 1.当Version一致时,执行成功version加1
userMapper.updateById(new User().setVersion(2).setName("小红").setId(1213659384740179970L));
/*
Preparing: UPDATE user SET NAME=?, VERSION=? WHERE ID=? AND VERSION=? AND DEL_FLAG=0
Parameters: 小红(String), 3(Integer), 1213659384740179970(Long), 2(Integer)
*/
// 2.当Version不一致时 Updates: 0
userMapper.updateById(new User().setVersion(2).setName("小蓝").setId(1213659384740179970L));
/*
Preparing: UPDATE user SET NAME=?, VERSION=? WHERE ID=? AND VERSION=? AND DEL_FLAG=0
Parameters: 小蓝(String), 3(Integer), 1213659384740179970(Long), 2(Integer)
*/
// 3.当不传Version,就不判断version
userMapper.updateById(new User().setName("小白").setId(1213659384740179970L));
/*
Preparing: UPDATE user SET NAME=? WHERE ID=? AND DEL_FLAG=0
Parameters: 小白(String), 1213659384740179970(Long)
*/
}