日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatisPlus插件扩展_OptimisticLockerInterceptor乐观锁插件的使用

發布時間:2025/3/19 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatisPlus插件扩展_OptimisticLockerInterceptor乐观锁插件的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

項目搭建專欄:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194

簡介

意圖:

當要更新一條記錄的時候,希望這條記錄沒有被別人更新

樂觀鎖實現方式:

  • 取出記錄時,獲取當前version
  • 更新時,帶上這個version
  • 執行更新時, set version = yourVersion+1 where version = yourVersion
  • 如果version不對,就更新失敗

實現

插件配置

來到項目下的applicationContext.xml中配置sqlSessionFactoryBean的地方。

<!--? 配置SqlSessionFactoryBeanMybatis提供的: org.mybatis.spring.SqlSessionFactoryBeanMP提供的:com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean--><bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"><!-- 數據源 --><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- 別名處理 --><property name="typeAliasesPackage" value="com.badao.beans"></property>?<!-- 注入全局MP策略配置 --><property name="globalConfig" ref="globalConfiguration"></property>?<!-- 插件注冊 --><property name="plugins"><list><!-- 注冊分頁插件 --><bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"></bean><!-- 注冊執行分析插件 --><bean class="com.baomidou.mybatisplus.plugins.SqlExplainInterceptor"><property name="stopProceed" value="true" /></bean><!-- 注冊性能分析插件 --><bean class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor"><!-- 單位為毫秒 --><!-- <property name="maxTime" value="30" /> --><!--SQL是否格式化 默認false--><property name="format" value="true" /></bean><!-- 注冊樂觀鎖插件 --><bean class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor"></bean></list></property>?</bean>

添加Version

1.實體類添加Version

@Version private Integer version;并生成set和get方法完整實體類package com.badao.beans;import java.io.Serializable;import com.baomidou.mybatisplus.activerecord.Model; import com.baomidou.mybatisplus.annotations.TableField; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.annotations.Version; import com.baomidou.mybatisplus.enums.IdType; import com.mchange.util.FailSuppressedMessageLogger;@TableName(value="employee") public class Employee? extends Model<Model>{@TableId(value="id",type=IdType.AUTO)private Integer id;//@TableField(value="last_name")private String name;private String email;private Integer gender;private Integer age;@TableField(exist=false)private String remark;@Versionprivate Integer version;public Integer getVersion() {return version;}public void setVersion(Integer version) {this.version = version;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender = gender;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}/**** 必須指定當前實體類的主鍵屬性*/@Overrideprotected Serializable pkVal() {// TODO Auto-generated method stubreturn id;}}

2.數據庫字段添加version

保存后給version賦值

3.測試

編寫測試方法

/****?? 樂觀鎖插件*/@Testpublic void testOptimisticLockerInterceptor() {//更新操作Employee employee = new Employee();employee.setId(3);employee.setName("OptimisticLockerInterceptor");employee.setAge(23);employee.setVersion(1);employeeMapper.updateById(employee);}


一定要設置版本!!!?employee.setVersion(1);

運行結果

此時版本統一會成功更新。

更新成功后數據庫的version字段會自動變成2。

假如此時另一個人也執行了更新操作,此時數據庫的version會變成3。

然后自己再次進行更新時使用version為2去進行更新,結果:

更新了0條。

源碼下載

https://download.csdn.net/download/badao_liumang_qizhi/11147706

?

?

總結

以上是生活随笔為你收集整理的MyBatisPlus插件扩展_OptimisticLockerInterceptor乐观锁插件的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。