Python 官方文档:入门教程 => 点击学习
目录1.@ 根据id更新 2.@ 条件构造器作为参数进行更新 3.@ lambda构造器 mybatisplus update语句为null时没有拼接上去1.@ 根据id更新
User user = new User();
user.setUserId(1);
user.setAge(29);
userMapper.updateById(user);
//把名字为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方法
//只更新一个属性,把名字为rhb的用户年龄更新为18,其他属性不变
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name","rhb").set("age", 18);
userMapper.update(null, updateWrapper);
LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(User::getName, "rhb").set(User::getAge, 18);
Integer rows = userMapper.update(null, lambdaUpdateWrapper);
我有一个设置页面,数据库就相当于是key和value的样子,当value为空的时候用updatebyId就变成了
update param where key=?
就没有set就会报语法错误
这个出现的场景是如果数据库本来改自己有值更新 null时不会有问题,当数据库也是null时更新就不会拼接set
数据库有值时update null
数据库也为空时的更新
然后查解决方案:mybatisplus为null的时候不会拼接,可配置一个策略updateStrategy = FieldStrategy.IGNORED无论是啥都会拼接 但是还是会有问题,指定下类型就可以了 最后经测试有两种方案可行
@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更新 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: mybatis-plus update更新操作的三种方式(小结)
本文链接: https://www.lsjlt.com/news/154944.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0