MyBatis-Plus update 行級鎖寫法


本想修改題目,但想到很多人看,應該說下問題:(2023-02-10更新)

1、下面只是MyBatis的update使用方法,不涉及行級鎖,這是當時認知錯誤。

2、行級鎖是在RR或RC隔離級別下,通過對索引項加鎖實現的。

3、因此update語句,需要在where條件使用索引檢索。

開門見山:(行級鎖是需要結合事務和索引優化的,並非通過代碼寫出來的)

LambdaUpdateWrapper<實體類> update = new LambdaUpdateWrapper<>();
update.setSql("a = a - 1 ");
update.setSql("a_status = case when (a = 0) then 1 else 0 end ");
update.eq(實體類::getId, id);
update.eq(實體類::aStatus, 1);
update.last("and a - 1 >= 0 ");
int execute = mapper.update(null, update);
if (1 != execute) throw new ServiceException();

說明:

1、setSql用於寫入復雜的修改語句

2、last用於寫入復雜的查詢語句

3、eq除了查詢ID,也要對修改前的狀態進行校驗

4、以上代碼僅用於示意,無具體邏輯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM