Java mybatis-plus集成乐观锁

相关概念

悲观锁(Pessimistic Lock)

每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁

乐观锁(Optimistic Lock)

每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号等机制

mybatis-plus集成乐观锁

实体类添加@Version

less 复制代码
/**
 * 版本号,用乐观锁
 */
@Version
private Integer version;

添加对乐观锁的支持

在启动类放入乐观锁的拦截器

java 复制代码
/**
 * mybatis-plus乐观锁支持
 * @return
 */
@Bean
public MybatisPlusInterceptor optimisticLockerInterceptor(){
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    return interceptor;
}
相关推荐
fliter21 小时前
Go设计取舍之一: goroutine 为什么保持匿名、无状态
后端
fliter21 小时前
Go设计取舍之二: maps.Keys和Values为什么返回迭代器
后端
热心市民lcj21 小时前
Spring Boot 整合 Caffeine 本地缓存实战
spring boot·后端·缓存
Revolution6121 小时前
Nest.js 是什么:怎样用它写出第一个后端接口
后端·node.js·nestjs
aiopencode21 小时前
SwiftUI Introspect生产环境完全指南:为什么它是安全可靠的选择
后端·ios
shengjk121 小时前
x86架构发展史:从8086到x86-64,一文看懂40多年CPU指令集如何改变世界
后端
JackSparrow4141 天前
前端安全之JS混淆+请求加密+请求签名以提升爬虫难度
前端·javascript·后端·爬虫·python·安全
geovindu1 天前
go:loghelper
开发语言·后端·golang
小满zs1 天前
Go语言第四章(类型转换)
后端·go
人间凡尔赛1 天前
告别冷启动!WebAssembly + Spin 实战:Serverless 延迟从 1 秒降到 1 毫秒
后端·云原生·serverless·webassembly·spin