我用的是3.0.5的mybatis-plus版本 但是我用3.4.0的OptimisticLockerInterceptor 会显示已过时但是还能用
按照乐观锁的步骤
模型上加属性
// @TableField(fill = FieldFill.INSERT) @Version private int version;然后数据库加上
对应的配置文件一定要有@Configuration注解别拼错 少了这一步就会报参数没找到 Parameter 'MP_OPTLOCK_VERSION_ORIGINAL' not found. Available parameters are [param1, et]
//开启事务 @EnableTransactionManagement @Configuration public class MybatisPlusConfig { @Bean public OptimisticLockerInterceptor optimisticLockerInterceptor() { return new OptimisticLockerInterceptor(); } }最后更新的时候需要先查询再根据查询出来的进行更新
@RequestMapping("/update") public int update(){ //乐观锁需要先查询再插入 User user=userServer.selectById(31); user.setUsername("test"); user.setAge(123); return userServer.updateById(user); }
