好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis-plus update更新操作的三种方式(小结)

1.@ 根据id更新

?

1

2

3

4

User user = new User();

user.setUserId( 1 );

user.setAge( 29 );

  userMapper.updateById(user);

2.@ 条件构造器作为参数进行更新

?

1

2

3

4

5

6

//把名字为rhb的用户年龄更新为18,其他属性不变

UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();

updateWrapper.eq( "name" , "rhb" );

User user = new User();

user.setAge( 18 );

userMapper.update(user, updateWrapper);

@ 假设只更新一个字段在使用updateWrapper 的构造器中也需要构造一个实体对象,这样比较麻烦。可以使用updateWrapper的set方法

?

1

2

3

4

//只更新一个属性,把名字为rhb的用户年龄更新为18,其他属性不变

UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();

updateWrapper.eq( "name" , "rhb" ).set( "age" , 18 );

userMapper.update( null , updateWrapper);

3.@ lambda构造器

?

1

2

3

LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

lambdaUpdateWrapper.eq(User::getName, "rhb" ).set(User::getAge, 18 );

Integer rows = userMapper.update( null , lambdaUpdateWrapper);

mybatisplus update语句为null时没有拼接上去

我有一个设置页面,数据库就相当于是key和value的样子,当value为空的时候用updatebyId就变成了

?

1

update param where key=?

就没有set就会报语法错误

这个出现的场景是如果数据库本来改自己有值更新 null时不会有问题,当数据库也是null时更新就不会拼接set
数据库有值时update null

数据库也为空时的更新

然后查解决方案:mybatisplus为null的时候不会拼接,可配置一个策略updateStrategy = FieldStrategy.IGNORED无论是啥都会拼接 但是还是会有问题,指定下类型就可以了 最后经测试有两种方案可行

?

1

2

3

@TableField (value = "PARAMVAL" ,updateStrategy = FieldStrategy.IGNORED,jdbcType = JdbcType.VARCHAR)

//@TableField(value = "PARAMVAL",jdbcType = JdbcType.VARCHAR, fill = FieldFill.UPDATE)

     private String paramVal;

以上两种方案均可

到此这篇关于mybatis-plus update更新操作的三种方式(小结)的文章就介绍到这了,更多相关mybatis-plus update更新 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! 

原文链接:https://blog.csdn.net/weixin_44162337/article/details/107828366

查看更多关于mybatis-plus update更新操作的三种方式(小结)的详细内容...

  阅读:71次