亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MyBatis Plus更新對象無法設(shè)空值解決方案

 更新時(shí)間:2020年11月23日 09:46:39   作者:_不正  
這篇文章主要介紹了MyBatis Plus更新對象無法設(shè)空值解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

原因

因?yàn)?MyBatis-Plus 自帶的更新方法,都有對對象空值進(jìn)行判空。只有不為空的字段才會(huì)進(jìn)行數(shù)據(jù)更新。

解決方式

在實(shí)體類對應(yīng)的字段上加注解@TableField(strategy=FieldStrategy.IGNORED),忽略null值的判斷,例如:

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

示例:

1、未加注解(無法設(shè)入空值,見代碼結(jié)果):

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

2、加注解(可以設(shè)入空值,看代碼結(jié)果)

//實(shí)體@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);
}
​
//結(jié)果
==> 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);
}
​
//結(jié)果
==> Preparing: UPDATE user SET address=? WHERE (id = ?) 
==> Parameters: null, 1(Integer)

附上 MyBatis-Plus 表字段標(biāo)識(shí) 注解類

/**
 * 表字段標(biāo)識(shí)
 *
 * @author hubin sjy tantan
 * @since 2016-09-09
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TableField {
​
  /**
   * 字段值(駝峰命名方式,該值可無)
   */
  String value() default "";
​
  /**
   * 是否為數(shù)據(jù)庫表字段
   * 默認(rèn) true 存在,false 不存在
   */
  boolean exist() default true;
​
  /**
   * 字段 where 實(shí)體查詢比較條件
   * 默認(rèn) `=` 等值
   */
  String condition() default "";
​
  /**
   * 字段 update set 部分注入, 該注解優(yōu)于 el 注解使用
   * <p>
   * 例1:@TableField(.. , update="%s+1") 其中 %s 會(huì)填充為字段
   * 輸出 SQL 為:update 表 set 字段=字段+1 where ...
   * <p>
   * 例2:@TableField(.. , update="now()") 使用數(shù)據(jù)庫時(shí)間
   * 輸出 SQL 為:update 表 set 字段=now() where ...
   */
  String update() default "";
​
  /**
   * 字段驗(yàn)證策略之 insert: 當(dāng)insert操作時(shí),該字段拼接insert語句時(shí)的策略
   * 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;
​
  /**
   * 字段驗(yàn)證策略之 update: 當(dāng)更新操作時(shí),該字段拼接set語句時(shí)的策略
   * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 屬性為null/空string都會(huì)被set進(jìn)去
   * 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;
​
  /**
   * 字段驗(yàn)證策略之 where: 表示該字段在拼接where條件時(shí)的策略
   * 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;
​
  /**
   * 字段自動(dòng)填充策略
   */
  FieldFill fill() default FieldFill.DEFAULT;
​
  /**
   * 是否進(jìn)行 select 查詢
   * <p>大字段可設(shè)置為 false 不加入 select 查詢范圍</p>
   */
  boolean select() default true;
​
  /**
   * 是否保持使用全局的 Format 的值
   * <p> 只生效于 既設(shè)置了全局的 Format 也設(shè)置了上面 {@link #value()} 的值 </p>
   * <li> 如果是 false , 全局的 Format 不生效 </li>
   *
   * @since 3.1.1
   */
  boolean keepGlobalFormat() default false;
​
  /**
   * JDBC類型 (該默認(rèn)值不代表會(huì)按照該值生效)
   * <p>
   * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}
   *
   * @since 3.1.2
   */
  JdbcType jdbcType() default JdbcType.UNDEFINED;
​
  /**
   * 類型處理器 (該默認(rèn)值不代表會(huì)按照該值生效)
   * <p>
   * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler}
   *
   * @since 3.1.2
   */
  Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
​
  /**
   * 指定小數(shù)點(diǎn)后保留的位數(shù)
   * <p>
   * {@link ParameterMapping#numericScale}
   *
   * @since 3.1.2
   */
  String numericScale() default "";
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論