解决 MyBatis-Plus 更新对象无法设空值

原因

因为 MyBatis-Plus 自带的更新方法,都有对对象空值进行判空。只有不为空的字段才会进行数据更新。

解决方式

在实体类对应的字段上加注解@TableField(strategy=FieldStrategy.IGNORED),忽略null值的判断,例如:

@TableField(updateStrategy = FieldStrategy.IGNORED)
private String address;

示例:

1、未加注解(无法设入空值,见代码结果):

//实体
private
String address;

@Test
public void updateUserTest(){ User user = new User(); user.setId(1); user.setState((byte) 1); user.setAddress(null); userService.updateById(user); } ​ //结果 ==> Preparing: UPDATE user SET state=? WHERE id=? ==> Parameters: 1(Byte), 1(Integer) ​

 

2、加注解(可以设入空值,看代码结果)

//实体
@TableField(updateStrategy = FieldStrategy.IGNORED) private String address;

@Test
public void updateUserTest(){ User user = new User(); user.setId(1); user.setState((byte) 1); user.setAddress(null); userService.updateById(user); } ​ //结果 ==> Preparing: UPDATE user SET address=?, state=? WHERE id=? ==> Parameters: null, 1(Byte), 1(Integer)

 

ps:注意配置文件中 mybatis-plus 中update-strategy 配置
#mybatis
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/**.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.xx.**.entity
  global-config:
    #数据库相关配置
    db-config:
#      select-strategy: not_empty
#      insert-strategy: not_empty
      update-strategy: not_empty
      #主键类型  AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
      id-type: ID_WORKER
    banner: false


ignored  不管有没有有设置属性,所有的字段都会设置到insert语句中,如果没设置值,全为null,这种在update 操作中会有风险,把有值的更新为null 

not_null,也是默认策略,也就是忽略null的字段,不忽略""

not-empty  为null,为空串的忽略,就是如果设置值为null,“”,不会插入数据库

 

 

 

 

3、直接使用 UpdateWrapper

@Test
public void updateUserTest(){
    UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
    userUpdateWrapper.set("address", null);
    userUpdateWrapper.lambda().eq(User::getId, 1);
    userService.update(userUpdateWrapper);
}
​
//结果
==>  Preparing: UPDATE user SET address=? WHERE (id = ?) 
==> Parameters: null, 1(Integer)

 

附上 MyBatis-Plus 表字段标识 注解类

/**
 * 表字段标识
 *
 * @author hubin sjy tantan
 * @since 2016-09-09
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TableField {
​
    /**
     * 字段值(驼峰命名方式,该值可无)
     */
    String value() default "";
​
    /**
     * 是否为数据库表字段
     * 默认 true 存在,false 不存在
     */
    boolean exist() default true;
​
    /**
     * 字段 where 实体查询比较条件
     * 默认 `=` 等值
     */
    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类型 (该默认值不代表会按照该值生效)
     * <p>
     * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}
     *
     * @since 3.1.2
     */
    JdbcType jdbcType() default JdbcType.UNDEFINED;
​
    /**
     * 类型处理器 (该默认值不代表会按照该值生效)
     * <p>
     * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler}
     *
     * @since 3.1.2
     */
    Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
​
    /**
     * 指定小数点后保留的位数
     * <p>
     * {@link ParameterMapping#numericScale}
     *
     * @since 3.1.2
     */
    String numericScale() default "";
}

 

 

posted @ 2020-05-16 15:32  _不正  阅读(15549)  评论(0编辑  收藏  举报