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;
}
相关推荐
Csvn15 小时前
Linux 系统性能监控与瓶颈排查
后端
铁皮饭盒15 小时前
Rust版Bun1.4之前, 盘点Bun1.3新特性
前端·javascript·后端
kfaino1 天前
码农的AI翻身(五)你好,我叫 Transformer
后端·aigc
Oneslide1 天前
机械革命 单系统纯净重装Ubuntu(全盘覆盖,清空原有Windows)
后端
GetcharZp1 天前
告别OOM!用Go+libvips实现30000×50000超大图片的流式瓦片服务
后端·go
IT_陈寒1 天前
JavaScript项目实战经验分享
前端·人工智能·后端
用户47949283569151 天前
6w star,GitHub 趋势第一的 Ponytail,这个agent插件到底在火什么
前端·后端
神奇小汤圆1 天前
2026一线大厂Java八股文精选(附答案,高质量整理)
后端