MyBatis-Plus update 行级锁写法
开门见山:
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、以上代码仅用于示意,无具体逻辑