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<TblEmployee> queryWrapper = new QueryWrapper<>();
LambdaQueryWrapper<TblEmployee> eq = queryWrapper.lambda().eq(TblEmployee::getCorpId, 2222);
List<TblEmployee> 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;
}
}