首页 > 其他分享 >mybatis-plus中的updateWrapper的用法

mybatis-plus中的updateWrapper的用法

时间:2023-01-07 19:23:37浏览次数:45  
标签:baseMapper 指定 修改 plus updateWrapper 设置 mybatis null user

如果使用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的记录进行获取,在将指定的字段进行修改

标签:baseMapper,指定,修改,plus,updateWrapper,设置,mybatis,null,user
From: https://www.cnblogs.com/just1t/p/17033306.html

相关文章