Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù)
Mybatis-Plus只更新部分字段數(shù)據(jù)
1、通過UpdateWrapper修改指定的列
update 時傳入 null 是關鍵
重點語句:updateWrapper.set(“SESSION_KEY”, “abc”);
this.mapper.update(null, wrapperUpdate);
2、使用場景和案例
使用版本:3.0.4
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("id", 1);
updateWrapper.set("status", ?1);
updateWrapper.set("nickname", ?"張三");
baseMapper.update(null, updateWrapper);Mybatis-Plus更新字段問題
mybatis更新為 null的對象 ,需設置 :
spring-boot設置
mybatis-plus.global-config.db-config.field-strategy=ignored
屬性設置有4種策略
public enum FieldStrategy {
? ? IGNORED, ? ? ? #忽略判斷
? ? NOT_NULL, ? ? ?#非 null 判斷
? ? NOT_EMPTY, ? ? # 非空判斷
? ? DEFAULT;
?
? ? private FieldStrategy() {
? ? }
}設置為忽略判斷之后 ,用查詢方法 :
public List<T> list(Wrapper<T> queryWrapper) {
? ? return this.baseMapper.selectList(queryWrapper);
}則查詢時候 :
SELECT id,level_code,scale,rate,interest,expire_date,factor_day,product_id,create_by,create_time,update_by,update_time,version,enable,remark FROM 表 WHERE level_code=null AND scale=null AND rate=null AND interest=null AND expire_date=null AND factor_day=null AND product_id=80 AND create_by=null AND create_time=null AND update_by=null AND update_time=null AND version=null AND enable=null AND remark=null
在mybatis-plus 3.1.2之后,fieldStrategy已被棄用 ,可以分別設置 :
selectStrategy(since 3.1.2) updateStrategy(since 3.1.2) insertStrategy(since 3.1.2)
官方文檔 : https://mp.baomidou.com/config/#logicdeletevalue
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用JPA主鍵@Id,@IdClass,@Embeddable,@EmbeddedId問題
這篇文章主要介紹了使用JPA主鍵@Id,@IdClass,@Embeddable,@EmbeddedId問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
解決java.lang.StringIndexOutOfBoundsException: String&nbs
這篇文章主要介紹了解決java.lang.StringIndexOutOfBoundsException: String index out of range: -1錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
詳解SpringBoot中RestTemplate的幾種實現(xiàn)
這篇文章主要介紹了詳解SpringBoot中RestTemplate的幾種實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11

