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 2022-08-26 15:44  何苦->  阅读(2412)  评论(0)    收藏  举报

导航