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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

_不正   2021-01-28 我要评论

原因

因为 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)

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 "";
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们