mybatisPlus更新字段值為null的解決方案
問(wèn)題描述
用Mybatis-Plus的update()或者updateById()來(lái)更新數(shù)據(jù)時(shí),無(wú)法將字段設(shè)置為null值(更新后數(shù)據(jù)還是原來(lái)的值)。
TableField源碼
/* * Copyright (c) 2011-2020, baomidou (jobob@qq.com). * <p> * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * <p> * https://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.mybatisplus.annotation; import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.mapping.ResultMapping; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.UnknownTypeHandler; import java.lang.annotation.*; /** * 表字段標(biāo)識(shí) * * @author hubin sjy tantan * @since 2016-09-09 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) public @interface TableField { /** * 數(shù)據(jù)庫(kù)字段值, * 不需要配置該值的情況: * <li> 當(dāng) {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 為 true 時(shí), * (mp下默認(rèn)是true,mybatis默認(rèn)是false), 數(shù)據(jù)庫(kù)字段值.replace("_","").toUpperCase() == 實(shí)體屬性名.toUpperCase() </li> * <li> 當(dāng) {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 為 false 時(shí), * 數(shù)據(jù)庫(kù)字段值.toUpperCase() == 實(shí)體屬性名.toUpperCase()</li> */ String value() default ""; /** * 是否為數(shù)據(jù)庫(kù)表字段 * 默認(rèn) true 存在,false 不存在 */ boolean exist() default true; /** * 字段 where 實(shí)體查詢(xún)比較條件 * 默認(rèn) {@link SqlCondition.EQUAL} */ String condition() default ""; /** * 字段 update set 部分注入, 該注解優(yōu)于 el 注解使用 * <p> * 例1:@TableField(.. , update="%s+1") 其中 %s 會(huì)填充為字段 * 輸出 SQL 為:update 表 set 字段=字段+1 where ... * <p> * 例2:@TableField(.. , update="now()") 使用數(shù)據(jù)庫(kù)時(shí)間 * 輸出 SQL 為:update 表 set 字段=now() where ... */ String update() default ""; /** * 字段驗(yàn)證策略之 insert: 當(dāng)insert操作時(shí),該字段拼接insert語(yǔ)句時(shí)的策略 * IGNORED: 直接拼接 insert into table_a(column) values (#{columnProperty}); * NOT_NULL: insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>) * NOT_EMPTY: insert into table_a(<if test="columnProperty != null and columnProperty!=''">column</if>) values (<if test="columnProperty != null and columnProperty!=''">#{columnProperty}</if>) * * @since 3.1.2 */ FieldStrategy insertStrategy() default FieldStrategy.DEFAULT; /** * 字段驗(yàn)證策略之 update: 當(dāng)更新操作時(shí),該字段拼接set語(yǔ)句時(shí)的策略 * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 屬性為null/空string都會(huì)被set進(jìn)去 * NOT_NULL: update table_a set <if test="columnProperty != null">column=#{columnProperty}</if> * NOT_EMPTY: update table_a set <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if> * * @since 3.1.2 */ FieldStrategy updateStrategy() default FieldStrategy.DEFAULT; /** * 字段驗(yàn)證策略之 where: 表示該字段在拼接where條件時(shí)的策略 * IGNORED: 直接拼接 column=#{columnProperty} * NOT_NULL: <if test="columnProperty != null">column=#{columnProperty}</if> * NOT_EMPTY: <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if> * * @since 3.1.2 */ FieldStrategy whereStrategy() default FieldStrategy.DEFAULT; /** * 字段自動(dòng)填充策略 */ FieldFill fill() default FieldFill.DEFAULT; /** * 是否進(jìn)行 select 查詢(xún) * <p>大字段可設(shè)置為 false 不加入 select 查詢(xún)范圍</p> */ boolean select() default true; /** * 是否保持使用全局的 Format 的值 * <p> 只生效于 既設(shè)置了全局的 Format 也設(shè)置了上面 {@link #value()} 的值 </p> * <li> 如果是 false , 全局的 Format 不生效 </li> * * @since 3.1.1 */ boolean keepGlobalFormat() default false; /** * JDBC類(lèi)型 (該默認(rèn)值不代表會(huì)按照該值生效), * 只生效與 mp 自動(dòng)注入的 method, * 建議配合 {@link TableName#autoResultMap()} 一起使用 * <p> * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType} * * @since 3.1.2 */ JdbcType jdbcType() default JdbcType.UNDEFINED; /** * 類(lèi)型處理器 (該默認(rèn)值不代表會(huì)按照該值生效), * 只生效與 mp 自動(dòng)注入的 method, * 建議配合 {@link TableName#autoResultMap()} 一起使用 * <p> * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler} * * @since 3.1.2 */ Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class; /** * 只在使用了 {@link #typeHandler()} 時(shí)判斷是否輔助追加 javaType * <p> * 一般情況下不推薦使用 * {@link ParameterMapping#javaType} * * @since 3.4.0 @2020-07-23 */ boolean javaType() default false; /** * 指定小數(shù)點(diǎn)后保留的位數(shù), * 只生效與 mp 自動(dòng)注入的 method, * 建議配合 {@link TableName#autoResultMap()} 一起使用 * <p> * {@link ParameterMapping#numericScale} * * @since 3.1.2 */ String numericScale() default ""; }
FieldStrategy 源碼
更新策略默認(rèn)是不為Null
/* * Copyright (c) 2011-2020, baomidou (jobob@qq.com). * <p> * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * <p> * https://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.mybatisplus.annotation; /** * 字段策略枚舉類(lèi) * <p> * 如果字段是基本數(shù)據(jù)類(lèi)型則最終效果等同于 {@link #IGNORED} * * @author hubin * @since 2016-09-09 */ public enum FieldStrategy { /** * 忽略判斷 */ IGNORED, /** * 非NULL判斷 */ NOT_NULL, /** * 非空判斷(只對(duì)字符串類(lèi)型字段,其他類(lèi)型字段依然為非NULL判斷) */ NOT_EMPTY, /** * 默認(rèn)的,一般只用于注解里 * <p>1. 在全局里代表 NOT_NULL</p> * <p>2. 在注解里代表 跟隨全局</p> */ DEFAULT, /** * 不加入 SQL */ NEVER }
設(shè)置為null的方案
使用UpdateWrapper更新
userService.lambdaUpdate() .eq(User::getId, user.getId()) .set(User::getUserName, user.getUserName()) .set(User::getNickName, null) .update();
設(shè)置全局的field-strategy(不推薦)
mybatis-plus: global-config: # 字段策略 0:忽略判斷,直接拼SQL, 1:非NULL, 2:非空,3:默認(rèn);4:永遠(yuǎn)不加入SQL field-strategy: 0
設(shè)置某個(gè)字段的field-strategy
在實(shí)體的某個(gè)字段上設(shè)置
@ApiModelProperty(value = "所在黨組織") @TableField(updateStrategy = FieldStrategy.IGNORED) private Long partyOrgId;
更新時(shí)直接將值設(shè)置為null
staffInfo.setPartyOrgId(null)
總結(jié)
到此這篇關(guān)于mybatisPlus更新字段值為null的文章就介紹到這了,更多相關(guān)mybatisPlus更新字段值為null內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot mybatis常見(jiàn)配置問(wèn)題解決
這篇文章主要介紹了Springboot mybatis常見(jiàn)配置問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11仿釘釘流程輕松實(shí)現(xiàn)JSON轉(zhuǎn)BPMN完整實(shí)現(xiàn)過(guò)程示例
這篇文章主要為大家介紹了仿釘釘流程輕松實(shí)現(xiàn)JSON轉(zhuǎn)BPMN完整實(shí)現(xiàn)過(guò)程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Spring核心思想之淺談IoC容器與依賴(lài)倒置(DI)
文章介紹了Spring的IoC和DI機(jī)制,以及MyBatis的動(dòng)態(tài)代理,通過(guò)注解和反射,Spring能夠自動(dòng)管理對(duì)象的創(chuàng)建和依賴(lài)注入,而MyBatis則通過(guò)動(dòng)態(tài)代理實(shí)現(xiàn)了接口方法到數(shù)據(jù)庫(kù)操作的映射,文章詳細(xì)解釋了Spring和MyBatis的工作原理,并通過(guò)示例代碼展示了它們的結(jié)合使用方式2025-01-01IntelliJ?IDEA?2023.1.4?無(wú)法刷新Maven項(xiàng)目模塊的問(wèn)題及解決方法
這篇文章主要介紹了如何排查?IDEA?自身報(bào)錯(cuò)問(wèn)題,本文以IntelliJ?IDEA?2023.1.4無(wú)法刷新項(xiàng)目Maven模塊的問(wèn)題為例,給大家詳細(xì)講解,需要的朋友可以參考下2023-08-08IDEA解決@Slf4j中l(wèi)og報(bào)紅問(wèn)題
在IntelliJ IDEA中使用log.info()時(shí),如果出現(xiàn)錯(cuò)誤,通常是因?yàn)槿鄙貺ombok插件,以下是解決方法:打開(kāi)IntelliJ IDEA,進(jìn)入設(shè)置(File > Settings 或者 Ctrl+Alt+S),在Plugins部分點(diǎn)擊Browse repositories,搜索Lombok并安裝,安裝完成后,問(wèn)題通??梢越鉀Q2024-12-12Java字節(jié)碼操縱框架ASM圖文實(shí)例詳解
這篇文章主要為大家介紹了Java字節(jié)碼操縱框架ASM圖文實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Mybatis用注解寫(xiě)in查詢(xún)的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis用注解寫(xiě)in查詢(xún)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07解決跨域請(qǐng)求,NG返回403(403并不一定是NG問(wèn)題)
這篇文章主要介紹了解決跨域請(qǐng)求,NG返回403(403并不一定是NG問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12