当前位置:   article > 正文

MyBatis-Plus---------悲观锁_mybatisplus 悲观锁

mybatisplus 悲观锁

本编参考视频:【尚硅谷】2022版MyBatisPlus教程

乐观锁

1.场景

一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小
李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太
高,可能会影响销量。又通知小王,你把商品价格降低30元。
此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王
也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据
库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就
完全被小王的覆盖了。
现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1
万多。

2.乐观锁与悲观锁

上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过
了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。
如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证
最终的价格是120元。

3.模拟修改冲突

数据库中增加商品表
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)
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
添加数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
  • 1
添加实体
@Data
public class Product {
private Long id;
private String name;
private Integer price;
private Integer version;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
添加mapper
public interface ProductMapper extends BaseMapper<Product> {
}
  • 1
  • 2

测试:

@Test
public void testConcurrentUpdate() {
	//1、小李
	Product p1 = productMapper.selectById(1L);
	System.out.println("小李取出的价格:" + p1.getPrice());// 100元
	//2、小王
	Product p2 = productMapper.selectById(1L);
	System.out.println("小王取出的价格:" + p2.getPrice());// 100元
	//3、小李将价格加了50元,存入了数据库
	p1.setPrice(p1.getPrice() + 50);
	productMapper.updateById(p1);// 150元
	
	//4、小王将商品减了30元,存入了数据库
	p2.setPrice(p2.getPrice() - 30);
	productMapper.updateById(p2);// 70元
	
	//最后的结果
	Product p3 = productMapper.selectById(1L);
	//价格覆盖,最后的结果:70
	System.out.println("最后的结果:" + p3.getPrice());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.乐观锁实现流程

数据库中添加version字段
取出记录时,获取当前version

SELECT id,`name`,price,`version` FROM product WHERE id=1
  • 1

更新时,version + 1,如果where语句中的version版本不对,则更新失败

UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id=1 AND`version`=1
  • 1

5.Mybatis-Plus实现乐观锁

修改实体类
@Data
public class Product {
private Long id;
private String name;
private Integer price;
@Version   //标识乐观锁版本号字段
private Integer version;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
添加乐观锁插件配置
@Configuration
//扫面指定的mapper包
@MapperScan("com.xin.mybatisplus2.mapper")
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
        //new PaginationInnerInterceptor(DbType.MYSQL):创建分页插件,数据库类型是MySQL
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        //添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
测试修改冲突

小李查询商品信息:
SELECT id,name,price,version FROM t_product WHERE id=?
小王查询商品信息:
SELECT id,name,price,version FROM t_product WHERE id=?
小李修改商品价格,自动将version+1
UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
Parameters: 外星人笔记本(String), 150(Integer), 1(Integer), 1(Long), 0(Integer)
小王修改商品价格,此时version已更新,条件不成立,修改失败
UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
Parameters: 外星人笔记本(String), 70(Integer), 1(Integer), 1(Long), 0(Integer)
最终,小王修改失败,查询价格:150
SELECT id,name,price,version FROM t_product WHERE id=?

优化流程
@Test
    public void testConcurrentUpdate() {
        Product productLi = productMapper.selectById(1L);
        System.out.println("小李取出的价格:"+productLi.getPrice());
        Product productWang=productMapper.selectById(1L);
        System.out.println("小王取出的价格:"+productWang.getPrice());
        //小李将商品价格+50
        productLi.setPrice(productLi.getPrice()+50);
        productMapper.updateById(productLi);
        //小王将商品价格-30
        productWang.setPrice(productWang.getPrice()-30);
        int result = productMapper.updateById(productWang);
        if(result==0) {//小王修改数据失败,重新查询
            Product productNew = productMapper.selectById(1L);
            productNew.setPrice(productNew.getPrice()-30);
            productMapper.updateById(productNew);
        }
        //老板查询价格
        Product productLaoBan=productMapper.selectById(1L);
        System.out.println("老板查询的价格:"+productLaoBan.getPrice());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

闽ICP备14008679号