mybatis-plus中的updateWrapper的用法

如果使用UpdateWrapper进行更新数据,默认的机制是将wrapper中设置的字段对应的值进行修改,如果值为null,则不会进行修改;
如果我们需要将指定的字段设置为null,需要在wrapper通过set()方法进行设置。

第一种情况:通过updateWrapper更新数据(不存在将信息保存为null)

场景:将指定UserId的记录的name字段进行修改

User user=new User();
user.setName("张三");//将将user中的name属性设置为张三
baseMapper.update(baseMapper.selectById(userId),new UpdateWrapper<User>(user));//通过id获取需要修改的记录,再进行修改

第二种情况:将指定字段中的值设置为NULL

场景:将指定UserId的记录的age字段设置为null

baseMapper.update(baseMapper.selectById(userId),new UpdateWrapper<User>().set("age",null));//将指定id的记录进行获取,在将指定的字段进行修改
posted @ 2023-01-07 19:07  just1t  阅读(14990)  评论(2编辑  收藏  举报