MyBatis-Plus⾃带的更新⽅法,都有对对象空值进⾏判空。只
有不为空的字段才会进⾏数据更新。
原因
因为MyBatis-Plus⾃带的更新⽅法,都有对对象空值进⾏判空。只有不为空的字段才会进⾏数据更新。
解决⽅式
在实体类对应的字段上加注解@TableField(strategy=D),忽略null值的判断,例如:
@TableField(updateStrategy=D)
privateStringaddress;
⽰例:
1、未加注解(⽆法设⼊空值,见代码结果):
//实体
privateStringaddress;
@Test
publicvoidupdateUrTest(){
Urur=newUr();
(1);
te((byte)1);
ress(null);
ById(ur);
}
//结果
==>Preparing:UPDATEurSETstate=?WHEREid=?
==>Parameters:1(Byte),1(Integer)
2、加注解(可以设⼊空值,看代码结果)
//实体
@TableField(updateStrategy=D)
privateStringaddress;
@Test
publicvoidupdateUrTest(){
Urur=newUr();
(1);
te((byte)1);
ress(null);
ById(ur);
}
//结果
==>Preparing:UPDATEurSETaddress=?,state=?WHEREid=?
==>Parameters:null,1(Byte),1(Integer)
3、直接使⽤UpdateWrapper
@Test
publicvoidupdateUrTest(){
UpdateWrapper
("address",null);
().eq(Ur::getId,1);
(urUpdateWrapper);
}
//结果
==>Preparing:UPDATEurSETaddress=?WHERE(id=?)
==>Parameters:null,1(Integer)
附上MyBatis-Plus表字段标识注解类
/**
*表字段标识
*
*@authorhubinsjytantan
*@since2016-09-09
*/
@Documented
@Retention(E)
@Target()
public@interfaceTableField{
/**
*字段值(驼峰命名⽅式,该值可⽆)
*/
Stringvalue()default"";
/**
*是否为数据库表字段
*默认true存在,fal不存在
*默认true存在,fal不存在
*/
booleanexist()defaulttrue;
/**
*字段where实体查询⽐较条件
*默认`=`等值
*/
Stringcondition()default"";
/**
*字段updatet部分注⼊,该注解优于el注解使⽤
*
*例1:@TableField(..,update="%s+1")其中%s会填充为字段
*输出SQL为:update表t字段=字段+1where...
*
*例2:@TableField(..,update="now()")使⽤数据库时间
*输出SQL为:update表t字段=now()where...
*/
Stringupdate()default"";
/**
*字段验证策略之inrt:当inrt操作时,该字段拼接inrt语句时的策略
*IGNORED:直接拼接inrtintotable_a(column)values(#{columnProperty});
*NOT_NULL:inrtintotable_a(
*NOT_EMPTY:inrtintotable_a(
columnProperty!=''">#{columnProperty})
*
*@since3.1.2
*/
FieldStrategyinrtStrategy()T;
/**
*字段验证策略之update:当更新操作时,该字段拼接t语句时的策略
*IGNORED:直接拼接updatetable_atcolumn=#{columnProperty},属性为null/空string都会被t进去
*NOT_NULL:updatetable_at
*NOT_EMPTY:updatetable_at
*
*@since3.1.2
*/
FieldStrategyupdateStrategy()T;
/**
*字段验证策略之where:表⽰该字段在拼接where条件时的策略
*IGNORED:直接拼接column=#{columnProperty}
*NOT_NULL:
*NOT_EMPTY:
*
*@since3.1.2
*/
FieldStrategywhereStrategy()T;
/**
*字段⾃动填充策略
*/
FieldFillfill()T;
/**
*是否进⾏lect查询
*
⼤字段可设置为fal不加⼊lect查询范围
*/
booleanlect()defaulttrue;
/**
*是否保持使⽤全局的Format的值
*
只⽣效于既设置了全局的Format也设置了上⾯{@link#value()}的值
*
*
*@since3.1.1
*/
booleankeepGlobalFormat()defaultfal;
/**
*JDBC类型(该默认值不代表会按照该值⽣效)
*
*{@linkResultMapping#jdbcType}and{@linkParameterMapping#jdbcType}
*
*@since3.1.2
*/
JdbcTypejdbcType()NED;
/**
*类型处理器(该默认值不代表会按照该值⽣效)
*
*{@linkResultMapping#typeHandler}and{@linkParameterMapping#typeHandler}
*
*@since3.1.2
*/
Class<?extendsTypeHandler>typeHandler();
/**
*指定⼩数点后保留的位数
*
*{@linkParameterMapping#numericScale}
*
*@since3.1.2
*/
StringnumericScale()default"";
}
本文发布于:2023-01-04 07:01:16,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/89340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |