今天小編給大家分享一下怎么使用MyBatisPlus+SpringBoot實現樂觀鎖功能的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
如果商城中有一件商品,成本價是80元,售價是100元。經理先是通知小李,說你去把商品價格增加50元。小李正在玩游戲,耽擱了一個小時。正好一個小時后,經理覺得商品價格增加到150元,價格太高,可能會影響銷量。又通知小王,你把商品價格降低30元。
此時,小李和小王同時操作商品后臺系統。小李操作的時候,系統先取出商品價格100元;小王也在操作,取出的商品價格也是100元。小李將價格加了50元,并將100+50=150元存入了數據庫;小王將商品減了30元,并將100-30=70元存入了數據庫。是的,如果沒有鎖,小李的操作就完全被小王的覆蓋了。
現在商品價格是70元,比成本價低10元。幾分鐘后,這個商品很快出售了1千多件商品,老板虧1萬多。
CREATE TABLE product ( id BIGINT(20) NOT NULL AUTO_INCREMENT 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 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> { }
@RunWith(SpringRunner.class) @SpringBootTest public class ProductVersionTest { @Resource private ProductMapper productMapper; @Test public void testProductUpdate() { //1、小李 Product p1 = productMapper.selectById(1L); //2、小王 Product p2 = productMapper.selectById(1L); //3、小李將價格加了50元,存入了數據庫 p1.setPrice(p1.getPrice() + 50); int result1 = productMapper.updateById(p1); System.out.println("小李修改結果:" + result1); //4、小王將商品減了30元,存入了數據庫 p2.setPrice(p2.getPrice() - 30); int result2 = productMapper.updateById(p2); System.out.println("小王修改結果:" + result2); //最后的結果 Product p3 = productMapper.selectById(1L); System.out.println("最后的結果:" + p3.getPrice()); } }
最后輸出的是 70元,與經理預期的120元不同,導致虧損,如何防止這樣的異常發生,解決方案是使用樂觀鎖
數據庫中添加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
添加 @Version 注解
@Version private Integer version;
@Configuration //@MapperScan("com.koo.modules.*.dao") public class MybatisPlusConfig { /** * 新的分頁插件,一緩和二緩遵循mybatis的規則,需要設置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現問題(該屬性會在舊插件移除后一同移除) */ @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.setUseDeprecatedExecutor(false); } }
(判斷第二次更新數據是否成功,不成功則重新取數據進行更新)
@RunWith(SpringRunner.class) @SpringBootTest public class ProductVersionTest { @Resource private ProductMapper productMapper; @Test public void testProductUpdate() { //1、小李 Product p1 = productMapper.selectById(1L); //2、小王 Product p2 = productMapper.selectById(1L); //3、小李將價格加了50元,存入了數據庫 p1.setPrice(p1.getPrice() + 50); int result1 = productMapper.updateById(p1); System.out.println("小李修改結果:" + result1); //4、小王將商品減了30元,存入了數據庫 p2.setPrice(p2.getPrice() - 30); int result2 = productMapper.updateById(p2); System.out.println("小王修改結果:" + result2); if(result2 == 0){//更新失敗,重試 System.out.println("小王重試"); //重新獲取數據 p2 = productMapper.selectById(1L); //更新 p2.setPrice(p2.getPrice() - 30); productMapper.updateById(p2); } //最后的結果 Product p3 = productMapper.selectById(1L); System.out.println("最后的結果:" + p3.getPrice()); } }
輸出結果為120,數據正確
以上就是“怎么使用MyBatisPlus+SpringBoot實現樂觀鎖功能”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。