本文源码:Gitee·点这里
使用乐观锁的意图是:当要更新一条记录的时候,希望这条记录没有被别人更新。那么需要在表中增加一个字段version来实现。
乐观锁实现方式:
- 取出记录时,获取当前version
- 更新时,带上这个version
- 执行更新时, set version = newVersion where version = oldVersion
- 如果version不对,就更新失败
而MyBatisPlus已经封装好了乐观锁的实现,我们来配置使用即可
乐观锁配置
配置乐观锁需要2步:
- 在
mybatisPlusInterceptor()
配置中,增加一行乐观锁插件配置即可:interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 分页插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件
return interceptor;
}
- 在实体类
version
字段中增加注解@Version
@Version
private Integer version;
测试代码
我们来试一下,将部门根据ID查询出来,修改其名称,并根据ID进行更新操作
@Test
public void testUpdateById() {
Dept dept = deptMapper.selectById("4b0e878b5bfc2f22f44f1a3691403116");
dept.setName("Lilei");
int result = deptMapper.updateById(dept);
System.out.println(result);
}
执行日志:
JDBC Connection [HikariProxyConnection@233271858 wrapping com.mysql.cj.jdbc.ConnectionImpl@62628e78] will not be managed by Spring
==> Preparing: SELECT id,name,deleted,version,create_time,update_time FROM dept WHERE id=? AND deleted=0
==> Parameters: 4b0e878b5bfc2f22f44f1a3691403116(String)
<== Columns: id, name, deleted, version, create_time, update_time
<== Row: 4b0e878b5bfc2f22f44f1a3691403116, Lilei, 0, 6, null, null
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@511505e7]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@743c6ce4] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@532297836 wrapping com.mysql.cj.jdbc.ConnectionImpl@62628e78] will not be managed by Spring
==> Preparing: UPDATE dept SET name=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: Lilei(String), 7(Integer), 4b0e878b5bfc2f22f44f1a3691403116(String), 6(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@743c6ce4]
1
从执行日志中可以看出会增加 set version = newVersion where version = oldVersion,表示乐观锁成功使用。为了进一步验证,我们模拟一个错误的执行
@Test
public void testUpdateById2() {
Dept dept1 = deptMapper.selectById("4b0e878b5bfc2f22f44f1a3691403116");
dept1.setName("Lilei1");
Dept dept2 = deptMapper.selectById("4b0e878b5bfc2f22f44f1a3691403116");
dept2.setName("Lilei2");
int result = deptMapper.updateById(dept1);
System.out.println(result);
int result2 = deptMapper.updateById(dept2);
System.out.println(result2);
}
执行日志:
JDBC Connection [HikariProxyConnection@1491623023 wrapping com.mysql.cj.jdbc.ConnectionImpl@a137d7a] will not be managed by Spring
==> Preparing: SELECT id,name,deleted,version,create_time,update_time FROM dept WHERE id=? AND deleted=0
==> Parameters: 4b0e878b5bfc2f22f44f1a3691403116(String)
<== Columns: id, name, deleted, version, create_time, update_time
<== Row: 4b0e878b5bfc2f22f44f1a3691403116, Lilei, 0, 7, null, null
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2a389173]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bbf9027] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@281151050 wrapping com.mysql.cj.jdbc.ConnectionImpl@a137d7a] will not be managed by Spring
==> Preparing: SELECT id,name,deleted,version,create_time,update_time FROM dept WHERE id=? AND deleted=0
==> Parameters: 4b0e878b5bfc2f22f44f1a3691403116(String)
<== Columns: id, name, deleted, version, create_time, update_time
<== Row: 4b0e878b5bfc2f22f44f1a3691403116, Lilei, 0, 7, null, null
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bbf9027]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@108d55c4] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@404152906 wrapping com.mysql.cj.jdbc.ConnectionImpl@a137d7a] will not be managed by Spring
==> Preparing: UPDATE dept SET name=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: Lilei1(String), 8(Integer), 4b0e878b5bfc2f22f44f1a3691403116(String), 7(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@108d55c4]
1
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7ee3d262] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@60221145 wrapping com.mysql.cj.jdbc.ConnectionImpl@a137d7a] will not be managed by Spring
==> Preparing: UPDATE dept SET name=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: Lilei2(String), 8(Integer), 4b0e878b5bfc2f22f44f1a3691403116(String), 7(Integer)
<== Updates: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7ee3d262]
0
从日志中我们可以看出,第一个update语句执行了,而第二个没有执行,验证成功。
特别说明
乐观锁特别说明:
- 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
- 整数类型下
newVersion = oldVersion + 1
-
newVersion
会回写到entity
中 - 仅支持
updateById(id)
与update(entity, wrapper)
方法 - 在
update(entity, wrapper)
方法下,wrapper
不能复用!!!