• MyBatisPlus(二十一)乐观锁


    使用场景

    用于当有多个用户同时修改同一条数据的时候,只允许有一个修改成功。

    实现原理

    使用一个字段,用于记录数据的版本。
    当修改数据时,会去检测当前版本是否是正在修改的版本,同时修改成功后会把 版本号 + 1

    实现方式

    1. 配置插件
    2. 在实体类的字段上加上@Version注解

    在这里插入图片描述

    代码

    配置插件

    package com.example.core.config;
    
    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @MapperScan("com.example.web")
    public class MybatisPlusConfig {
    
        /**
         * 添加拦截器
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件
            interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); // 针对 update 和 delete 语句 作用: 阻止恶意的全表更新删除
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 如果配置多个插件,切记分页最后添加
            // interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
            return interceptor;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在实体类的字段上加上@Version注解

    package com.example.web.entity;
    
    import com.baomidou.mybatisplus.annotation.Version;
    import lombok.Data;
    
    @Data
    
    public class User {
    
        // 其他字段
    
        /**
         * 版本
         */
        @Version
        private Integer version;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    数据库表

    在这里插入图片描述

    测试

    代码

        /**
         * 插入用户
         */
        @Test
        public void insert() {
            User user = new User();
            user.setId(16L);
            user.setName("郑一");
            user.setAge(30);
            user.setEmail("zhengyi@example.com");
            user.setGender(GenderEnum.MALE);
    
            mapper.insert(user);
        }
    
    
        /**
         * 更新用户:版本号为空,乐观锁失效。
         */
        @Test
        public void update() {
            User user = new User();
            user.setId(16L);
            user.setAge(31);
    
            mapper.updateById(user);
        }
    
    
        /**
         * 更新用户:版本号 +1(版本号不为空,乐观锁有效)。
         */
        @Test
        public void updateWithVersion() {
            User user = mapper.selectById(16L);
            user.setAge(31);
    
            mapper.updateById(user);
        }
    
    
        /**
         * 更新用户:测试同时更新,第二个更新失败。
         */
        @Test
        public void updateConcurrent() {
            // 同步查询
            User user1 = mapper.selectById(16L);
            user1.setAge(32);
    
            User user2 = mapper.selectById(16L);
            user2.setAge(33);
    
            // 更新
            mapper.updateById(user1);
            mapper.updateById(user2);
        }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    新插入的数据

    在这里插入图片描述

    更新时版本号为空,乐观锁失效

    感觉这像是一个漏洞。
    在这里插入图片描述

    更新时版本号不为空,乐观锁有效

    当 实体 的 version 字段不为空时,乐观锁才能正常生效。
    在这里插入图片描述

    模拟同时更新

    同一条数据,查询两次出来。然后调用两次更新,第一次更新成功,第二次更新失败。

    查询两次数据

    在这里插入图片描述

    两次更新,第一次成功了,Updates: 1;第二次失败了,Updates: 0

    在这里插入图片描述

  • 相关阅读:
    课程思政案例
    Kyligence李栋:从数据湖到指标中台,提升数据分析ROI
    AdaBoost 算法:理解、实现和掌握 AdaBoost
    Mybatis—缓存
    C++编程规范(一)
    程序员自由创业周记#2:前期准备
    Flink(二)
    华为交换机创建端口组
    微信小程序商城如何构建私域流量池?
    KT148A语音芯片组合播放之间有间隔不连贯的处理方法V1
  • 原文地址:https://blog.csdn.net/sgx1825192/article/details/133935594