mybatisPlus更新字段值为null怎么解决

其他教程   发布日期:2025年02月04日   浏览次数:209

这篇文章主要介绍“mybatisPlus更新字段值为null怎么解决”,在日常操作中,相信很多人在mybatisPlus更新字段值为null怎么解决问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mybatisPlus更新字段值为null怎么解决”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    问题描述

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

    TableField源码

    1. /*
    2. * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
    3. * <p>
    4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    5. * use this file except in compliance with the License. You may obtain a copy of
    6. * the License at
    7. * <p>
    8. * https://www.apache.org/licenses/LICENSE-2.0
    9. * <p>
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13. * License for the specific language governing permissions and limitations under
    14. * the License.
    15. */
    16. package com.baomidou.mybatisplus.annotation;
    17. import org.apache.ibatis.mapping.ParameterMapping;
    18. import org.apache.ibatis.mapping.ResultMapping;
    19. import org.apache.ibatis.type.JdbcType;
    20. import org.apache.ibatis.type.TypeHandler;
    21. import org.apache.ibatis.type.UnknownTypeHandler;
    22. import java.lang.annotation.*;
    23. /**
    24. * 表字段标识
    25. *
    26. * @author hubin sjy tantan
    27. * @since 2016-09-09
    28. */
    29. @Documented
    30. @Retention(RetentionPolicy.RUNTIME)
    31. @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
    32. public @interface TableField {
    33. /**
    34. * 数据库字段值,
    35. * 不需要配置该值的情况:
    36. * <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 true 时,
    37. * (mp下默认是true,mybatis默认是false), 数据库字段值.replace("_","").toUpperCase() == 实体属性名.toUpperCase() </li>
    38. * <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 false 时,
    39. * 数据库字段值.toUpperCase() == 实体属性名.toUpperCase()</li>
    40. */
    41. String value() default "";
    42. /**
    43. * 是否为数据库表字段
    44. * 默认 true 存在,false 不存在
    45. */
    46. boolean exist() default true;
    47. /**
    48. * 字段 where 实体查询比较条件
    49. * 默认 {@link SqlCondition.EQUAL}
    50. */
    51. String condition() default "";
    52. /**
    53. * 字段 update set 部分注入, 该注解优于 el 注解使用
    54. * <p>
    55. * 例1:@TableField(.. , update="%s+1") 其中 %s 会填充为字段
    56. * 输出 SQL 为:update 表 set 字段=字段+1 where ...
    57. * <p>
    58. * 例2:@TableField(.. , update="now()") 使用数据库时间
    59. * 输出 SQL 为:update 表 set 字段=now() where ...
    60. */
    61. String update() default "";
    62. /**
    63. * 字段验证策略之 insert: 当insert操作时,该字段拼接insert语句时的策略
    64. * IGNORED: 直接拼接 insert into table_a(column) values (#{columnProperty});
    65. * NOT_NULL: insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
    66. * NOT_EMPTY: insert into table_a(<if test="columnProperty != null and columnProperty!=''">column</if>) values (<if test="columnProperty != null and columnProperty!=''">#{columnProperty}</if>)
    67. *
    68. * @since 3.1.2
    69. */
    70. FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;
    71. /**
    72. * 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略
    73. * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去
    74. * NOT_NULL: update table_a set <if test="columnProperty != null">column=#{columnProperty}</if>
    75. * NOT_EMPTY: update table_a set <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
    76. *
    77. * @since 3.1.2
    78. */
    79. FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;
    80. /**
    81. * 字段验证策略之 where: 表示该字段在拼接where条件时的策略
    82. * IGNORED: 直接拼接 column=#{columnProperty}
    83. * NOT_NULL: <if test="columnProperty != null">column=#{columnProperty}</if>
    84. * NOT_EMPTY: <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
    85. *
    86. * @since 3.1.2
    87. */
    88. FieldStrategy whereStrategy() default FieldStrategy.DEFAULT;
    89. /**
    90. * 字段自动填充策略
    91. */
    92. FieldFill fill() default FieldFill.DEFAULT;
    93. /**
    94. * 是否进行 select 查询
    95. * <p>大字段可设置为 false 不加入 select 查询范围</p>
    96. */
    97. boolean select() default true;
    98. /**
    99. * 是否保持使用全局的 Format 的值
    100. * <p> 只生效于 既设置了全局的 Format 也设置了上面 {@link #value()} 的值 </p>
    101. * <li> 如果是 false , 全局的 Format 不生效 </li>
    102. *
    103. * @since 3.1.1
    104. */
    105. boolean keepGlobalFormat() default false;
    106. /**
    107. * JDBC类型 (该默认值不代表会按照该值生效),
    108. * 只生效与 mp 自动注入的 method,
    109. * 建议配合 {@link TableName#autoResultMap()} 一起使用
    110. * <p>
    111. * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}
    112. *
    113. * @since 3.1.2
    114. */
    115. JdbcType jdbcType() default JdbcType.UNDEFINED;
    116. /**
    117. * 类型处理器 (该默认值不代表会按照该值生效),
    118. * 只生效与 mp 自动注入的 method,
    119. * 建议配合 {@link TableName#autoResultMap()} 一起使用
    120. * <p>
    121. * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler}
    122. *
    123. * @since 3.1.2
    124. */
    125. Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
    126. /**
    127. * 只在使用了 {@link #typeHandler()} 时判断是否辅助追加 javaType
    128. * <p>
    129. * 一般情况下不推荐使用
    130. * {@link ParameterMapping#javaType}
    131. *
    132. * @since 3.4.0 @2020-07-23
    133. */
    134. boolean javaType() default false;
    135. /**
    136. * 指定小数点后保留的位数,
    137. * 只生效与 mp 自动注入的 method,
    138. * 建议配合 {@link TableName#autoResultMap()} 一起使用
    139. * <p>
    140. * {@link ParameterMapping#numericScale}
    141. *
    142. * @since 3.1.2
    143. */
    144. String numericScale() default "";
    145. }

    FieldStrategy 源码

    更新策略默认是不为Null

    1. /*
    2. * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
    3. * <p>
    4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    5. * use this file except in compliance with the License. You may obtain a copy of
    6. * the License at
    7. * <p>
    8. * https://www.apache.org/licenses/LICENSE-2.0
    9. * <p>
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13. * License for the specific language governing permissions and limitations under
    14. * the License.
    15. */
    16. package com.baomidou.mybatisplus.annotation;
    17. /**
    18. * 字段策略枚举类
    19. * <p>
    20. * 如果字段是基本数据类型则最终效果等同于 {@link #IGNORED}
    21. *
    22. * @author hubin
    23. * @since 2016-09-09
    24. */
    25. public enum FieldStrategy {
    26. /**
    27. * 忽略判断
    28. */
    29. IGNORED,
    30. /**
    31. * 非NULL判断
    32. */
    33. NOT_NULL,
    34. /**
    35. * 非空判断(只对字符串类型字段,其他类型字段依然为非NULL判断)
    36. */
    37. NOT_EMPTY,
    38. /**
    39. * 默认的,一般只用于注解里
    40. * <p>1. 在全局里代表 NOT_NULL</p>
    41. * <p>2. 在注解里代表 跟随全局</p>
    42. */
    43. DEFAULT,
    44. /**
    45. * 不加入 SQL
    46. */
    47. NEVER
    48. }

    设置为null的方案

    使用UpdateWrapper更新

    1. userService.lambdaUpdate()
    2. .eq(User::getId, user.getId())
    3. .set(User::getUserName, user.getUserName())
    4. .set(User::getNickName, null)
    5. .update();

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

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

    设置某个字段的field-strategy

    在实体的某个字段上设置

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

    更新时直接将值设置为null

    1. staffInfo.setPartyOrgId(null)

    以上就是mybatisPlus更新字段值为null怎么解决的详细内容,更多关于mybatisPlus更新字段值为null怎么解决的资料请关注九品源码其它相关文章!