赞
踩
当要更新一条记录的时候,希望这条记录无法被别人更新,也就是说实现线程安全的数据更新。
取出记录时,获取当前 version
更新时,带上这个version
执行更新时, set version = newVersion where version = oldVersion
如果 version 不对,就更新失败
张三和李四更新同一条记录,如果不加乐观锁,谁后更新以谁的数据为准。加上乐观锁后,谁先抢到乐观锁,就以谁的更新为准,其他用户更新无效。
- CREATE TABLE `user` (
- `id` bigint(20) NOT NULL DEFAULT '0' COMMENT '主键ID',
- `NAME` varchar(30) DEFAULT NULL COMMENT '姓名',
- `age` int(11) DEFAULT NULL COMMENT '年龄',
- `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
- `create_time` datetime DEFAULT NULL,
- `update_time` datetime DEFAULT NULL,
- `version` int(11) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- @Data
- public class User {
- @TableId(type = IdType.ASSIGN_ID)
- private Long id;
-
- private String name;
- private Integer age;
- private String email;
-
- @TableField(fill = FieldFill.INSERT)
- private Date createTime; // create_time
-
- @TableField(fill = FieldFill.INSERT_UPDATE)
- private Date updateTime; // update_time
-
- @Version
- @TableField(fill = FieldFill.INSERT)
- private Integer version;
- }
- @Configuration
- @MapperScan("com.cakin.demomptest.mapper")
- public class MpConfig {
- /**
- * 乐观锁插件
- */
- @Bean
- public OptimisticLockerInterceptor optimisticLockerInterceptor() {
- return new OptimisticLockerInterceptor();
- }
- }
- // mp执行添加操作,这个方法执行
- @Override
- public void insertFill(MetaObject metaObject) {
- this.setFieldValByName("createTime",new Date(),metaObject);
- this.setFieldValByName("updateTime",new Date(),metaObject);
- this.setFieldValByName("version",1,metaObject);
- }
先添加一条数据
- // 添加一条数据
- @Test
- public void testAdd() {
- User user = new User();
- user.setName("乐观锁");
- user.setAge(20);
- user.setEmail("1243@qq.com");
- int insert = userMapper.insert(user);
- System.out.println(insert);
- }
添加后数据库如下
- mysql> select * from user;
- +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
- | id | NAME | age | email | create_time | update_time | version |
- +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
- | 0 | auto | 20 | 1243@qq.com | NULL | NULL | NULL |
- | 1 | Jone | 18 | test1@baomidou.com | NULL | NULL | NULL |
- | 2 | Jack | 20 | test2@baomidou.com | NULL | NULL | NULL |
- | 3 | Tom | 28 | test3@baomidou.com | NULL | NULL | NULL |
- | 4 | Sandy | 21 | test4@baomidou.com | NULL | NULL | NULL |
- | 5 | Billie | 24 | test5@baomidou.com | NULL | NULL | NULL |
- | 1443158688033337346 | lucymaryupup | 20 | 1243@qq.com | NULL | 2021-09-30 08:51:12 | NULL |
- | 1443378040145903617 | ASSIGN_ID | 20 | 1243@qq.com | 2021-09-30 08:51:56 | 2021-09-30 08:51:56 | NULL |
- | 1444192337759502338 | 乐观锁 | 20 | 1243@qq.com | 2021-10-02 14:47:40 | 2021-10-02 14:47:40 | 1 |
- +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
- 9 rows in set (0.00 sec)
执行乐观锁的测试
- // 测试乐观锁
- @Test
- public void testOptimisticLocker() {
- // 根据id查询
- User user = userMapper.selectById(1444192337759502338L);
- // 修改
- user.setName("zhangsan");
- userMapper.updateById(user);
- }
执行后数据库如下
- mysql> select * from user;
- +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
- | id | NAME | age | email | create_time | update_time | version |
- +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
- | 0 | auto | 20 | 1243@qq.com | NULL | NULL | NULL |
- | 1 | Jone | 18 | test1@baomidou.com | NULL | NULL | NULL |
- | 2 | Jack | 20 | test2@baomidou.com | NULL | NULL | NULL |
- | 3 | Tom | 28 | test3@baomidou.com | NULL | NULL | NULL |
- | 4 | Sandy | 21 | test4@baomidou.com | NULL | NULL | NULL |
- | 5 | Billie | 24 | test5@baomidou.com | NULL | NULL | NULL |
- | 1443158688033337346 | lucymaryupup | 20 | 1243@qq.com | NULL | 2021-09-30 08:51:12 | NULL |
- | 1443378040145903617 | ASSIGN_ID | 20 | 1243@qq.com | 2021-09-30 08:51:56 | 2021-09-30 08:51:56 | NULL |
- | 1444192337759502338 | zhangsan | 20 | 1243@qq.com | 2021-10-02 14:47:40 | 2021-10-02 14:55:10 | 2 |
- +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
- 9 rows in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。