当前位置:   article > 正文

MyBatis-Plus 实现乐观锁_mybatisplus 锁定记录

mybatisplus 锁定记录

一 点睛

1 主要适用场景

当要更新一条记录的时候,希望这条记录无法被别人更新,也就是说实现线程安全的数据更新。

2 乐观锁实现方式

取出记录时,获取当前 version

更新时,带上这个version

执行更新时, set version = newVersion where version = oldVersion

如果 version 不对,就更新失败

3 图例

张三和李四更新同一条记录,如果不加乐观锁,谁后更新以谁的数据为准。加上乐观锁后,谁先抢到乐观锁,就以谁的更新为准,其他用户更新无效。

二 实战

1 主要流程见下图

2 数据库 

  1. CREATE TABLE `user` (
  2. `id` bigint(20) NOT NULL DEFAULT '0' COMMENT '主键ID',
  3. `NAME` varchar(30) DEFAULT NULL COMMENT '姓名',
  4. `age` int(11) DEFAULT NULL COMMENT '年龄',
  5. `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
  6. `create_time` datetime DEFAULT NULL,
  7. `update_time` datetime DEFAULT NULL,
  8. `version` int(11) DEFAULT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 3 实体类

  1. @Data
  2. public class User {
  3. @TableId(type = IdType.ASSIGN_ID)
  4. private Long id;
  5. private String name;
  6. private Integer age;
  7. private String email;
  8. @TableField(fill = FieldFill.INSERT)
  9. private Date createTime; // create_time
  10. @TableField(fill = FieldFill.INSERT_UPDATE)
  11. private Date updateTime; // update_time
  12. @Version
  13. @TableField(fill = FieldFill.INSERT)
  14. private Integer version;
  15. }

4 配置乐观锁

  1. @Configuration
  2. @MapperScan("com.cakin.demomptest.mapper")
  3. public class MpConfig {
  4. /**
  5. * 乐观锁插件
  6. */
  7. @Bean
  8. public OptimisticLockerInterceptor optimisticLockerInterceptor() {
  9. return new OptimisticLockerInterceptor();
  10. }
  11. }

5 插入时,让版本的默认值为1

  1. // mp执行添加操作,这个方法执行
  2. @Override
  3. public void insertFill(MetaObject metaObject) {
  4. this.setFieldValByName("createTime",new Date(),metaObject);
  5. this.setFieldValByName("updateTime",new Date(),metaObject);
  6. this.setFieldValByName("version",1,metaObject);
  7. }

6 测试

先添加一条数据

  1. // 添加一条数据
  2. @Test
  3. public void testAdd() {
  4. User user = new User();
  5. user.setName("乐观锁");
  6. user.setAge(20);
  7. user.setEmail("1243@qq.com");
  8. int insert = userMapper.insert(user);
  9. System.out.println(insert);
  10. }

添加后数据库如下

  1. mysql> select * from user;
  2. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  3. | id | NAME | age | email | create_time | update_time | version |
  4. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  5. | 0 | auto | 20 | 1243@qq.com | NULL | NULL | NULL |
  6. | 1 | Jone | 18 | test1@baomidou.com | NULL | NULL | NULL |
  7. | 2 | Jack | 20 | test2@baomidou.com | NULL | NULL | NULL |
  8. | 3 | Tom | 28 | test3@baomidou.com | NULL | NULL | NULL |
  9. | 4 | Sandy | 21 | test4@baomidou.com | NULL | NULL | NULL |
  10. | 5 | Billie | 24 | test5@baomidou.com | NULL | NULL | NULL |
  11. | 1443158688033337346 | lucymaryupup | 20 | 1243@qq.com | NULL | 2021-09-30 08:51:12 | NULL |
  12. | 1443378040145903617 | ASSIGN_ID | 20 | 1243@qq.com | 2021-09-30 08:51:56 | 2021-09-30 08:51:56 | NULL |
  13. | 1444192337759502338 | 乐观锁 | 20 | 1243@qq.com | 2021-10-02 14:47:40 | 2021-10-02 14:47:40 | 1 |
  14. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  15. 9 rows in set (0.00 sec)

执行乐观锁的测试

  1. // 测试乐观锁
  2. @Test
  3. public void testOptimisticLocker() {
  4. // 根据id查询
  5. User user = userMapper.selectById(1444192337759502338L);
  6. // 修改
  7. user.setName("zhangsan");
  8. userMapper.updateById(user);
  9. }

执行后数据库如下

  1. mysql> select * from user;
  2. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  3. | id | NAME | age | email | create_time | update_time | version |
  4. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  5. | 0 | auto | 20 | 1243@qq.com | NULL | NULL | NULL |
  6. | 1 | Jone | 18 | test1@baomidou.com | NULL | NULL | NULL |
  7. | 2 | Jack | 20 | test2@baomidou.com | NULL | NULL | NULL |
  8. | 3 | Tom | 28 | test3@baomidou.com | NULL | NULL | NULL |
  9. | 4 | Sandy | 21 | test4@baomidou.com | NULL | NULL | NULL |
  10. | 5 | Billie | 24 | test5@baomidou.com | NULL | NULL | NULL |
  11. | 1443158688033337346 | lucymaryupup | 20 | 1243@qq.com | NULL | 2021-09-30 08:51:12 | NULL |
  12. | 1443378040145903617 | ASSIGN_ID | 20 | 1243@qq.com | 2021-09-30 08:51:56 | 2021-09-30 08:51:56 | NULL |
  13. | 1444192337759502338 | zhangsan | 20 | 1243@qq.com | 2021-10-02 14:47:40 | 2021-10-02 14:55:10 | 2 |
  14. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  15. 9 rows in set (0.00 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/435946
推荐阅读
相关标签
  

闽ICP备14008679号