mybatis-plus
mybatis-plus copied to clipboard
将某些字段更新为null的策略,FieldStrategy 是否能提供多一种策略
当前使用版本(必填,否则不予处理)
3.4.3.4
该问题是如何引起的?(确定最新版也有问题再提!!!)
设置字段更新策略为FieldStrategy.IGNORED,使得可以更新字段为null, 但这样也使得这个类其他地方使用updateById的时候,必须把原来的数据内容先查出来,再放到类里update 例如,我给User的attr1,attr2,attr3字段设置更新策略为FieldStrategy.IGNORED,其他字段attr4,attr5,attr6,attr7为默认策略, 当我更新User的attr4-7时 我需要把attr1-3查出来
public void updateUser(UpdateUser updateUser){
User dbUser = this.getById(updateUser.getId());
User newUser = updateUser.buildUser();
newUser.setAttr1(dbUser.getAttr1());
newUser.setAttr2(dbUser.getAttr2());
newUser.setAttr3(dbUser.getAttr3());
this.updateById(newUser)
}
这样,每次都去重新查出来,写起来有些不适 是不是可以多提供一种策略 提供JDK里所有数据类型的默认值,如果等于默认值就设置为null 当我想设置Attr1-3 为null的时候,把这些值设置进去,
public final Integer intNull = new Integer(0)// 不一定是0,随便什么数都可以
public final Integer stringNull = new String("qazwsxedcrfv")// 由于String.intern()的存在,需要找一串足够冷门的字符串
public final Integer dateNull = new Date(0)// 不一定是0,随便什么数都可以
public void updateUser(UpdateUser updateUser){
dbUser = this.getById(id);
User newUser = updateUser.buildUser();
newUser.setAttr1(intNull);
newUser.setAttr2(stringNull);
newUser.setAttr3(dateNull);
this.updateById(newUser)
}
框架内部检测到和intNull地址相等,就认为是要设置为null 这样的话,在类的其他字段为null的时候,还是能享受 default策略下 “为null就忽略” 的丝滑