MyBatis Plus更新對象無法設空值解決方案
原因
因為 MyBatis-Plus 自帶的更新方法,都有對對象空值進行判空。只有不為空的字段才會進行數據更新。
解決方式
在實體類對應的字段上加注解@TableField(strategy=FieldStrategy.IGNORED),忽略null值的判斷,例如:
@TableField(updateStrategy = FieldStrategy.IGNORED)private String address;
示例:
1、未加注解(無法設入空值,見代碼結果):
//實體private String address;@Testpublic 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;@Testpublic 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
@Testpublic 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 '';}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
