The new version of Mybatis-Plus uses optimistic lock, how to use MP_OPTLOCK_VERSION_ORIGINAL and the new version of the plug-in
In the old version of the configuration class, we usually register directly.
// 注册乐观锁插件
@Bean
public OptimisticLockerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInterceptor();
}
Then in version 3.4.0, this usage was abandoned, and we can see the comments provided by the source code.
Old version:
New version: The
new version adds MybatisPlusInterceptor, which is equivalent to the total interceptor class. Put the original classes on it as internal classes. , Using the same idea as spring
We can use this method to configure the previous plug-in, and it is still a List collection, we can configure multiple plug-ins at the same time, take the optimistic lock plug-in and the paging plug-in for example:
// 注册乐观锁插件与分页插件
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//分页插件
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//乐观锁插件
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
The old version needs to register multiple beans to configure
// 注册乐观锁插件
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
// 分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
In this way, optimistic lock and paging plugins can be used normally, the same is true for other plugins