栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatisplus乐观锁与悲观锁

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatisplus乐观锁与悲观锁

c>模拟修改冲突 创建数据
CREATE TABLE t_product
(
    id BIGINT(20) NOT NULL COMMENT '主键id',
    NAME VARCHAr(30) NULL DEFAULT NULL COMMENT '商品名称',
    price INT(11) DEFAULT 0 COMMENT '价格',
    VERSION INT(11) DEFAULT 0 COMMENT '乐观锁版本号',
    PRIMARY KEY (id)
);

插入一条数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人', 100);

在pojo包下创建Product类 
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName("t_product")
public class Product {
    private Long id;
    private String name;
    private Integer price;
    private Integer version;
}
 在mapper包下创建ProductMapper接口
@Repository
@Mapper
public interface ProductMapper extends BaseMapper {
}
冲突测试:
    @Autowired
    private ProductMapper productMapper;

    @Test
    public void testProduct(){
        //小李查询价格
        Product productLi = productMapper.selectById(1);
        System.out.println("小李查询的商品价格:"+productLi.getPrice());
        //小王查询价格
        Product productWang = productMapper.selectById(1);
        System.out.println("小王查询的商品价格:"+productWang.getPrice());
        //小李将商品价格+50
        productLi.setPrice(productLi.getPrice()+50);
        productMapper.updateById(productLi);
        //小王将商品价格-30
        productWang.setPrice(productWang.getPrice()-30);
        productMapper.updateById(productWang);

        //老板查询商品价格
        Product productLaoan = productMapper.selectById(1);
        System.out.println("老板查询的商品价格:"+productLaoan.getPrice());
    }
结果:

不对

 d

e>mybatisplus实现乐观锁

说明:

  • 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
  • 整数类型下 newVersion = oldVersion + 1
  • newVersion 会回写到 entity 中
  • 仅支持 updateById(id) 与 update(entity, wrapper) 方法
  • 在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
修改实体类,给乐观锁版本号添加注解@Version表示乐观锁版本号字段
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName("t_product")
public class Product {
    private Long id;
    private String name;
    private Integer price;
    @Version
    //表示乐观锁版本号字段
    private Integer version;
}
乐观锁插件配置: spring xml 方式:



    
        
            
        
    

spring boot 注解方式:

在java文件夹创建一个配置文件夹config,在里面创建一个插件配置类mybatisplusconfig,添加插件

package config;

@Configuration
@MapperScan(basePackages = {"mapper"})//引导类扫描定义的接口
public class MybatisPlusConfig {
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        //乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> configuration.setUsetUseDeprecatedExecutor(false);
//    }

}
测试:

同上

结果:

依然不对

 优化测试:
    @Autowired
    private ProductMapper productMapper;

    @Test
    public void testProduct(){
        //小李查询价格
        Product productLi = productMapper.selectById(1);
        System.out.println("小李查询的商品价格:"+productLi.getPrice());
        //小王查询价格
        Product productWang = productMapper.selectById(1);
        System.out.println("小王查询的商品价格:"+productWang.getPrice());
        //小李将商品价格+50
        productLi.setPrice(productLi.getPrice()+50);
        productMapper.updateById(productLi);
        //小王将商品价格-30
        productWang.setPrice(productWang.getPrice()-30);
        //判断修改的版本是否正确,result=0表示修改的版本错误,即有人刚修改了内容
        int result=productMapper.updateById(productWang);
        if (result==0){
            //重新获取版本号,修改内容
            Product productNew=productMapper.selectById(1);
            productNew.setPrice(productNew.getPrice()-30);
            productMapper.updateById(productNew);
        }

        //老板查询商品价格
        Product productLaoan = productMapper.selectById(1);
        System.out.println("老板查询的商品价格:"+productLaoan.getPrice());
    }
结果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/888324.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号