MyBatis-Plus update 行级锁写法

本想修改题目,但想到很多人看,应该说下问题:(2023-02-10更新)

1、下面只是MyBatis的update使用方法,不涉及行级锁,这是当时认知错误。

2、行级锁是在RR或RC隔离级别下,通过对索引项加锁实现的。

3、因此update语句,需要在where条件使用索引检索。

开门见山:(行级锁是需要结合事务和索引优化的,并非通过代码写出来的)

LambdaUpdateWrapper<实体类> update = new LambdaUpdateWrapper<>();
update.setSql("a = a - 1 ");
update.setSql("a_status = case when (a = 0) then 1 else 0 end ");
update.eq(实体类::getId, id);
update.eq(实体类::aStatus, 1);
update.last("and a - 1 >= 0 ");
int execute = mapper.update(null, update);
if (1 != execute) throw new ServiceException();

说明:

1、setSql用于写入复杂的修改语句

2、last用于写入复杂的查询语句

3、eq除了查询ID,也要对修改前的状态进行校验

4、以上代码仅用于示意,无具体逻辑

posted @ 2022-03-31 09:42  御简  阅读(2650)  评论(0编辑  收藏  举报