1. 程式人生 > 實用技巧 >【記錄】解決 MyBatis-Plus 更新物件無法設空值

【記錄】解決 MyBatis-Plus 更新物件無法設空值

參考地址:https://www.cnblogs.com/buzheng/p/12900736.html

原因

因為 MyBatis-Plus 自帶的更新方法,都有對物件空值進行判空。只有不為空的欄位才會進行資料更新。

解決方式

方式一:(不建議)

mybatis-plus:
  global-config:
    db-config:
      field-strategy: ignored

方式二:在實體類對應的欄位上加註解@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 "";
}