声明,目前只是仅仅针对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;

最新文章

  1. linux epoll模型使用注意点
  2. Topcoder SRM 619 DIv2 500 --又是耻辱的一题
  3. 将Nagios监控信息存入Mysql
  4. Solaris系统管理(二)资源管理与网络配置
  5. java测试1
  6. BIND9的架构与机制笔记1
  7. 用Jfree实现条形柱状图表,java代码实现
  8. BZOJ_5180_[Baltic2016]Cities_ 斯坦纳树
  9. 洛谷P3398 仓鼠找suger
  10. 【hdu 5628】Clarke and math (Dirichlet卷积)
  11. Adjacent Bit Counts(01组合数)
  12. 《Thinkphp5使用Socket服务》 入门篇
  13. oracle中有关初始化参数文件的几个视图对比
  14. PHP中正则表达式函数(Perl兼容)
  15. sqlplus执行startup出现ORA-00119,ORA-00132错误
  16. .NetCore Cap 结合 RabbitMQ 实现消息订阅
  17. chorme插件 ,在浏览器上模拟手机,pad 查看网页|前端技术开发必备插件
  18. Qt编写activex控件在网页中运行
  19. NBUT 1224 Happiness Hotel 2010辽宁省赛
  20. Linux tree 命令乱码

热门文章

  1. 批处理TOMCAT8.0自动重启任务
  2. Only fullscreen activities can request orientation
  3. SparseArray类
  4. python实现贪婪算法解决01背包问题
  5. 与ServletContext相关的监听器
  6. Python+Selenium笔记(十六)屏幕截图
  7. 2016年,谁是最受欢迎的 Java EE 服务器?
  8. LeetCode题解之Binary Tree Tilt
  9. She Left Her Shoes
  10. 树莓派Pi2 使用入门