MP实战系列(十七)之乐观锁插件
2024-09-26 05:34:44
声明,目前只是仅仅针对3.0以下版本,2.0以上版本。
意图:
当要更新一条记录的时候,希望这条记录没有被别人更新
乐观锁实现方式:
- 取出记录时,获取当前version
- 更新时,带上这个version
- 执行更新时, set version = yourVersion+1 where version = yourVersion
- 如果version不对,就更新失败
第一步,配置
SSM框架(Spring+SpringMVC+MyBatis-Plus):
spring-mybatis.xml配置:
<bean class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor"/>
SpringBoot配置:
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
第二步,加上注解
public class User { @Version
private Integer version; }
特别说明: 仅支持int,Integer,long,Long,Date,Timestamp
测试代码:
int id = 100;
int version = 2; User u = new User();
u.setId(id);
u.setVersion(version); if(userService.updateById(u)){
System.out.println("Update successfully");
}else{
System.out.println("Update failed due to modified by others");
}
示例SQL原理:
update tbl_user set name='update',version=3 where id=100 and version=2;
最新文章
- linux epoll模型使用注意点
- Topcoder SRM 619 DIv2 500 --又是耻辱的一题
- 将Nagios监控信息存入Mysql
- Solaris系统管理(二)资源管理与网络配置
- java测试1
- BIND9的架构与机制笔记1
- 用Jfree实现条形柱状图表,java代码实现
- BZOJ_5180_[Baltic2016]Cities_ 斯坦纳树
- 洛谷P3398 仓鼠找suger
- 【hdu 5628】Clarke and math (Dirichlet卷积)
- Adjacent Bit Counts(01组合数)
- 《Thinkphp5使用Socket服务》 入门篇
- oracle中有关初始化参数文件的几个视图对比
- PHP中正则表达式函数(Perl兼容)
- sqlplus执行startup出现ORA-00119,ORA-00132错误
- .NetCore Cap 结合 RabbitMQ 实现消息订阅
- chorme插件 ,在浏览器上模拟手机,pad 查看网页|前端技术开发必备插件
- Qt编写activex控件在网页中运行
- NBUT 1224 Happiness Hotel 2010辽宁省赛
- Linux tree 命令乱码