mybatisPlus更新字段值为null

  • 问题描述
  • TableField源码
  • FieldStrategy 源码
  • 设置为null的方案
    • 使用UpdateWrapper更新
    • 设置全局的field-strategy(不推荐)
    • 设置某个字段的field-strategy

问题描述

用Mybatis-Plus的update()或者updateById()来更新数据时,无法将字段设置为null值(更新后数据还是原来的值)。

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.*;/*** 表字段标识** @author hubin sjy tantan* @since 2016-09-09*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface TableField {/*** 数据库字段值,* 不需要配置该值的情况:* <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 true 时,* (mp下默认是true,mybatis默认是false), 数据库字段值.replace("_","").toUpperCase() == 实体属性名.toUpperCase() </li>* <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 false 时,* 数据库字段值.toUpperCase() == 实体属性名.toUpperCase()</li>*/String value() default "";/*** 是否为数据库表字段* 默认 true 存在,false 不存在*/boolean exist() default true;/*** 字段 where 实体查询比较条件* 默认 {@link SqlCondition.EQUAL}*/String condition() default "";/*** 字段 update set 部分注入, 该注解优于 el 注解使用* <p>* 例1:@TableField(.. , update="%s+1") 其中 %s 会填充为字段* 输出 SQL 为:update 表 set 字段=字段+1 where ...* <p>* 例2:@TableField(.. , update="now()") 使用数据库时间* 输出 SQL 为:update 表 set 字段=now() where ...*/String update() default "";/*** 字段验证策略之 insert: 当insert操作时,该字段拼接insert语句时的策略* 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;/*** 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略* IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去* 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;/*** 字段验证策略之 where: 表示该字段在拼接where条件时的策略* 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;/*** 字段自动填充策略*/FieldFill fill() default FieldFill.DEFAULT;/*** 是否进行 select 查询* <p>大字段可设置为 false 不加入 select 查询范围</p>*/boolean select() default true;/*** 是否保持使用全局的 Format 的值* <p> 只生效于 既设置了全局的 Format 也设置了上面 {@link #value()} 的值 </p>* <li> 如果是 false , 全局的 Format 不生效 </li>** @since 3.1.1*/boolean keepGlobalFormat() default false;/*** JDBC类型 (该默认值不代表会按照该值生效),* 只生效与 mp 自动注入的 method,* 建议配合 {@link TableName#autoResultMap()} 一起使用* <p>* {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}** @since 3.1.2*/JdbcType jdbcType() default JdbcType.UNDEFINED;/*** 类型处理器 (该默认值不代表会按照该值生效),* 只生效与 mp 自动注入的 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()} 时判断是否辅助追加 javaType* <p>* 一般情况下不推荐使用* {@link ParameterMapping#javaType}** @since 3.4.0 @2020-07-23*/boolean javaType() default false;/*** 指定小数点后保留的位数,* 只生效与 mp 自动注入的 method,* 建议配合 {@link TableName#autoResultMap()} 一起使用* <p>* {@link ParameterMapping#numericScale}** @since 3.1.2*/String numericScale() default "";
}

FieldStrategy 源码

更新策略默认是不为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;/*** 字段策略枚举类* <p>* 如果字段是基本数据类型则最终效果等同于 {@link #IGNORED}** @author hubin* @since 2016-09-09*/
public enum FieldStrategy {/*** 忽略判断*/IGNORED,/*** 非NULL判断*/NOT_NULL,/*** 非空判断(只对字符串类型字段,其他类型字段依然为非NULL判断)*/NOT_EMPTY,/*** 默认的,一般只用于注解里* <p>1. 在全局里代表 NOT_NULL</p>* <p>2. 在注解里代表 跟随全局</p>*/DEFAULT,/*** 不加入 SQL*/NEVER
}

设置为null的方案

使用UpdateWrapper更新

userService.lambdaUpdate().eq(User::getId, user.getId()).set(User::getUserName, user.getUserName()).set(User::getNickName, null).update();

设置全局的field-strategy(不推荐)

mybatis-plus:global-config:# 字段策略 0:忽略判断,直接拼SQL, 1:非NULL, 2:非空,3:默认;4:永远不加入SQLfield-strategy: 0

设置某个字段的field-strategy

在实体的某个字段上设置

@ApiModelProperty(value = "所在党组织")@TableField(updateStrategy = FieldStrategy.IGNORED)private Long partyOrgId;

更新时直接将值设置为null


staffInfo.setPartyOrgId(null)

mybatisPlus更新字段值为null相关推荐

  1. Mybatis-plus更新字段为null

    Mybatis-plus更新字段为null 1.在实体类的属性上增加注解:@TableField(updateStrategy = FieldStrategy.IGNORED) 2.使用LambdaU ...

  2. 【spring data jpa】spring data jpa 中的update 更新字段,如果原字段值为null不处理,不为null则在原来的值上加一段字符串...

    示例代码: /*** 如果barCode字段值为null则不处理* 如果barCode字段值不为null则在原本值的前面拼接 del:* @param dealer* @return*/@Modify ...

  3. 删除mysql所有字段值为NULL的数据

    今天在玩mysql的时候,填了很多字段值没有数据的数据,一条一条删太麻烦了 这种方式删除不了 delete from 表名 where 字段 = NULL 使用这种方式就可以了 delete from ...

  4. mysql length函数无法对字段值为null的筛选

    mysql中length(articletype)<5 不包含articletype 的值为null

  5. ASP.NET-解决GridView控件某字段设置只读后,更新后该字段值为Null

    用GridView控件自带的更新命令时,总会有一些字段不允许用户更改的.可以将该字段的ReadOnly属性设置为true. 但是,点击更新后,会将该字段的值设置为null. 这是因为在SqlDataS ...

  6. java json 修改字段_JSON文件-Java:编辑/更新字段值

    我的工作流程中有一些JSONObject,并且通过将它们写入json文件来存储相同的JSONObject. 我想要一种有效的方式来更新json文件, 仅 更新 需要的字段, 以及更新的JSONObje ...

  7. 查询某个字段值为null的记录

    Selec * From repairCode is null 另外如果要查询字段中未赋值的记录 可用 SELECT * FROM repairCode='' (无空格) 在SQL SERVER的企业 ...

  8. SQL Server 批量更新字段值为ROW_NUMBER()+列名称

    摘要:有这样一个需求,需要把数据表中的列名称的数据按行号重新排序并更新一下,这里用到了ROW_NUMBER()函数. 一:需求如下图: 二:通过执行如下sql语句实现了上面的需求,如下: update ...

  9. mysql 升序 字段值为NULL 排在后面

    select * from yryz_products_t order by isnull(sort),sort; 转载于:https://www.cnblogs.com/austinspark-je ...

最新文章

  1. ef core mysql 字符集,EF Core 基础知识
  2. [JZOJ5459]【NOIP2017提高A组冲刺11.7】密室
  3. 【ajax 】同步、异步交互流程的区别
  4. 架构师已死(转自UML软件工程组织)
  5. go 打印bool_Golang语言基础教程:键盘输入和打印输出
  6. .NET Framework 4.8预览
  7. [html] 你是如何理解html与css分离的?
  8. vscode配置python 控制台/终端/TERMINAL 不输出/不显示 解决办法
  9. C++ select模型聊天室初版
  10. Live2D在Unity中的使用
  11. iOS Segue使用
  12. 微信公众号开发--自定义菜单跳转页面并获取用户信息
  13. php中hexdec,PHP hexdec()函数
  14. (附源码)node.js物资管理系统 毕业设计 071130
  15. 解决FAT32格式U盘安装win10时0x8007000D错误
  16. Python之django框架模型(models)详解
  17. Spire.Doc 获取批注的源头+书签+内容
  18. linux命令 du -h --max-depth=0,查看当前目录下文件大小
  19. 域名升级访问中拿笔记好_域名选择与老域名质量评分,尽量少踩坑
  20. Android作业——简单的联系人

热门文章

  1. Java中文处理学习笔记——Hello Unicode (转)
  2. In App Purchases(IAP 应用程序內购买): 完全攻略 (2合1)
  3. 购车的选中功能 iOS
  4. 二、婴儿用品信息数据初了解
  5. Nginx 基本概念(反向代理、动静分离、负载均衡、高可用)、安装配置(JDK\Tomcat\Nginx\Keepalived)、配置实例效果
  6. 一维数组实验题:计算平均数、中位数和众数 在调查数据分析(Survey data analysis)中经常需要计算平均数、中位数和众数。用函数编程计算40个输入数据(是取值1—10之间的任意整数)的平
  7. dw怎样用css设置背景图片,dreamweaver
  8. HTB soccer
  9. 计算机毕业设计ssm教师授课信息综合管理平台451gu系统+程序+源码+lw+远程部署
  10. vb4android 源码,android-Q pixel4 xl 源代码下载