欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis-plus update更新操作的三種方式(小結(jié))

 更新時(shí)間:2021年10月20日 11:09:22   作者:波神小波  
本文主要介紹了mybatis-plus update更新操作的三種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1.@ 根據(jù)id更新

User user = new User();
user.setUserId(1);
user.setAge(29);
 userMapper.updateById(user);

2.@ 條件構(gòu)造器作為參數(shù)進(jìn)行更新

//把名字為rhb的用戶年齡更新為18,其他屬性不變
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name","rhb");
User user = new User();
user.setAge(18);
userMapper.update(user, updateWrapper);

@ 假設(shè)只更新一個(gè)字段在使用updateWrapper 的構(gòu)造器中也需要構(gòu)造一個(gè)實(shí)體對(duì)象,這樣比較麻煩??梢允褂胾pdateWrapper的set方法

//只更新一個(gè)屬性,把名字為rhb的用戶年齡更新為18,其他屬性不變
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name","rhb").set("age", 18);
userMapper.update(null, updateWrapper);

3.@ lambda構(gòu)造器

LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(User::getName, "rhb").set(User::getAge, 18);
Integer rows = userMapper.update(null, lambdaUpdateWrapper);

mybatisplus update語句為null時(shí)沒有拼接上去

我有一個(gè)設(shè)置頁面,數(shù)據(jù)庫就相當(dāng)于是key和value的樣子,當(dāng)value為空的時(shí)候用updatebyId就變成了

update param where key=?

就沒有set就會(huì)報(bào)語法錯(cuò)誤

這個(gè)出現(xiàn)的場景是如果數(shù)據(jù)庫本來改自己有值更新 null時(shí)不會(huì)有問題,當(dāng)數(shù)據(jù)庫也是null時(shí)更新就不會(huì)拼接set
數(shù)據(jù)庫有值時(shí)update null

在這里插入圖片描述

數(shù)據(jù)庫也為空時(shí)的更新

在這里插入圖片描述

然后查解決方案:mybatisplus為null的時(shí)候不會(huì)拼接,可配置一個(gè)策略u(píng)pdateStrategy = FieldStrategy.IGNORED無論是啥都會(huì)拼接 但是還是會(huì)有問題,指定下類型就可以了 最后經(jīng)測試有兩種方案可行

@TableField(value = "PARAMVAL",updateStrategy = FieldStrategy.IGNORED,jdbcType = JdbcType.VARCHAR)
//@TableField(value = "PARAMVAL",jdbcType = JdbcType.VARCHAR, fill = FieldFill.UPDATE)
    private String paramVal;

以上兩種方案均可

到此這篇關(guān)于mybatis-plus update更新操作的三種方式(小結(jié))的文章就介紹到這了,更多相關(guān)mybatis-plus update更新 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論