栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

MyBatisPlus 乐观锁注解@Version注解

MyBatisPlus 乐观锁注解@Version注解

MyBatisPlus 乐观锁注解@Version注解 Version

描述:乐观锁注解、标记 @Verison 在字段上

MybatisPlus有一个乐观锁注解,在使用的时候遇到一些问题。

乐观锁的字段在基类中,模型如下:

@Data
public class Tblbase {
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;

    private Date createTime;

    @Version
    private Date lastUpdateTime;

    private String enableFlag;
}

更新代码如下:

@Test
public void update() {
    QueryWrapper queryWrapper = new QueryWrapper<>();
    LambdaQueryWrapper eq = queryWrapper.lambda().eq(TblEmployee::getCorpId, 2222);
    List tblEmployees = dao.selectList(eq);
    tblEmployees.forEach(p -> {
        p.setEmployeeNumber(String.format("P%s", p.getEmployeeNumber()));
        dao.updateById(p);
    });
}

运行后发现抛异常:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'MP_OPTLOCK_VERSION_ORIGINAL' not found. Available parameters are [param1, et]

	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)

需要注入一个拦截器

@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {

     @Bean
     public MybatisPlusInterceptor optimisticLockerInterceptor() {
         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
         interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); 
         return interceptor;
     }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/706537.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号