mybatisPlus将实体字段更新为null值失效

bug描述

User user = userMapper.selectById("123");
user.setPassword(null);
userMapper.updateById(user);
//更新失败

查询mybatisPlus文档和部分源码后,发现updateById()这个方法只更新传入实体有值的字段

/**
 * 根据 ID 更新有值字段
 *
 * @author hubin
 * @since 2018-04-06
 */
public class UpdateById extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
       *****
    }
}

所以应改成用update(),在条件构造器上将需置空的字段设置为空后更新

User user = userMapper.selectById("123");
userMapper.update(user,Wrapper.lambdaUpdate(user)
							  .set(User::getPassword,null)
							  .eq(User::getId,user.getId()));

你可能感兴趣的:(BUG日记)