赞
踩
本编参考视频:【尚硅谷】2022版MyBatisPlus教程
一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小
李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太
高,可能会影响销量。又通知小王,你把商品价格降低30元。
此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王
也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据
库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就
完全被小王的覆盖了。
现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1
万多。
上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过
了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。
如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证
最终的价格是120元。
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);
@Data
public class Product {
private Long id;
private String name;
private Integer price;
private Integer version;
}
public interface ProductMapper extends BaseMapper<Product> {
}
测试:
@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()); }
数据库中添加version字段
取出记录时,获取当前version
SELECT id,`name`,price,`version` FROM product WHERE id=1
更新时,version + 1,如果where语句中的version版本不对,则更新失败
UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id=1 AND`version`=1
@Data
public class Product {
private Long id;
private String name;
private Integer price;
@Version //标识乐观锁版本号字段
private Integer version;
}
@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;
}
}
小李查询商品信息:
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()); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。