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