[MyBatisPlus]乐观锁和悲观锁
生活随笔
收集整理的這篇文章主要介紹了
[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);添加實體
package com.xxxx.mybatisplus.pojo;import lombok.Data;@Data public class Product {private Long id;private String name;private Integer price;private Integer version; }ProductMapper
package com.xxxx.mybatisplus.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.xxxx.mybatisplus.pojo.Product; import org.springframework.stereotype.Repository;@Repository public interface ProductMapper extends BaseMapper<Product> {}測試
package com.xxxx.mybatisplus;import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.xxxx.mybatisplus.mapper.ProductMapper; import com.xxxx.mybatisplus.mapper.UserMapper; import com.xxxx.mybatisplus.pojo.Product; import com.xxxx.mybatisplus.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest public class MyBatisPlusPluginsTest {@Autowiredprivate UserMapper userMapper;@Autowiredprivate ProductMapper productMapper;@Testpublic void testProduct01(){// 小李查詢商品價格Product productLi = productMapper.selectById(1);System.out.println("小李查詢的商品價格 = "+productLi.getPrice());// 100// 小王查詢商品價格Product productWang = productMapper.selectById(1);System.out.println("小王查詢的商品價格 = "+productWang.getPrice());// 100// 小李將商品價格+50productLi.setPrice(productLi.getPrice() + 50);productMapper.updateById(productLi);// 小王將商品價格-30productWang.setPrice(productWang.getPrice()-30);productMapper.updateById(productWang);// 老板查詢商品價格Product productBoss = productMapper.selectById(1);System.out.println("老板查詢的商品價格 = "+productBoss.getPrice());// 70}}樂觀鎖實現流程
數據庫中添加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`=1Mybatis-Plus實現樂觀鎖
修改實體類
修改配置類
測試
優化修改流程
@Testpublic void testProduct01(){// 小李查詢商品價格Product productLi = productMapper.selectById(1);System.out.println("小李查詢的商品價格 = "+productLi.getPrice());// 100// 小王查詢商品價格Product productWang = productMapper.selectById(1);System.out.println("小王查詢的商品價格 = "+productWang.getPrice());// 100// 小李將商品價格+50productLi.setPrice(productLi.getPrice() + 50);productMapper.updateById(productLi);// 小王將商品價格-30productWang.setPrice(productWang.getPrice()-30);int result = productMapper.updateById(productWang);if (result == 0){// 操作失敗,重試Product productNew = productMapper.selectById(1);productNew.setPrice(productNew.getPrice() - 30);productMapper.updateById(productNew);}// 老板查詢商品價格Product productBoss = productMapper.selectById(1);System.out.println("老板查詢的商品價格 = "+productBoss.getPrice());// 120}總結
以上是生活随笔為你收集整理的[MyBatisPlus]乐观锁和悲观锁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机QQ扫码快速登录功能以及好友权限设置
- 下一篇: [SpringBoot2]welcome