Parameter 'MP_OPTLOCK_VERSION_ORIGINAL' not found. Available parameters are [param1, et]

我用的是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();
    }
}

也可以这么写

/**
 * mybatisplus分页插件和乐观锁
 * @data 2021/9/14
 **/
@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}

最后更新的时候需要先查询再根据查询出来的进行更新

    @RequestMapping("/update")
    public int update(){
        //乐观锁需要先查询再插入
        User user=userServer.selectById(31);
        user.setUsername("test");
        user.setAge(123);
        return userServer.updateById(user);
    }

当配置以上后,mybatis-plus分页会失效 此时还要增加一个拦截器 注意版本,写的方式不同


@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
		return interceptor;
	}
}

posted on   何苦->  阅读(2117)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示