mybatis-plus中的updateById 方法,正常情況下,如果設(shè)置字段為null,由于默認的字段策略,不會操作此字段,不會變?yōu)閚ull值,那應(yīng)該怎么做的?
第一種: 不推薦
上面我說了,默認的字段策略會忽略掉,所以我們可以將要設(shè)置為null 的字段的策略更改下.例如;
@TableField(strategy = FieldStrategy.IGNORED)
private LocalDateTime offlineTime;
這樣再次調(diào)用updateById,就ok了
由于做了這個更改,可能后續(xù)會有其他人誤操作此表的時候,真的將這個字段不知情的情況下置為了null,可能造成嚴(yán)重事故!!!
第二種: 推薦方式
這種也相對簡單,就是用mybatis-plus自帶的updatewrapper
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(User::getOfflineTime,null);
updateWrapper.set(User::getContent,null);
updateWrapper.eq(User::getId,article.getId());
userMapper.update(user, updateWrapper);
利用自帶的update方法即可,這樣就能實現(xiàn)字段變?yōu)閚ull了!!