Mybatis-Plus updateById方法更新無(wú)效及空值處理過程
在使用 Mybatis-Plus 進(jìn)行數(shù)據(jù)持久化操作時(shí),updateById 方法默認(rèn)不會(huì)更新字段的空值(null)。
這是因?yàn)?Mybatis-Plus 為了防止誤操作,避免將數(shù)據(jù)庫(kù)中原本存在的非空字段更新為 null。然而,在某些業(yè)務(wù)場(chǎng)景下,我們可能需要允許更新空值。
以下是幾種解決 updateById 方法不更新空值或更新字段無(wú)效問題的方法:
1. 使用UpdateWrapper并設(shè)置setSqlSelect
通過 UpdateWrapper 可以靈活地控制更新的字段,包括允許更新為 null。
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
// 假設(shè)有一個(gè)實(shí)體類 User
User user = new User();
user.setId(1); // 需要更新的記錄ID
user.setName(null); // 需要更新為空的字段
user.setAge(30);
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", user.getId())
.set("name", user.getName()) // 允許 name 字段更新為 null
.set("age", user.getAge());
int rows = userMapper.update(null, updateWrapper);
System.out.println("受影響的行數(shù): " + rows);
2. 使用LambdaUpdateWrapper并調(diào)用set方法
LambdaUpdateWrapper 提供了類型安全的更新方式,同樣可以設(shè)置字段為 null。
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
User user = new User();
user.setId(1);
user.setName(null); // 需要更新為空的字段
user.setAge(30);
LambdaUpdateWrapper<User> lambdaUpdate = new LambdaUpdateWrapper<>();
lambdaUpdate.eq(User::getId, user.getId())
.set(User::getName, user.getName()) // 允許 name 字段更新為 null
.set(User::getAge, user.getAge());
int rows = userMapper.update(null, lambdaUpdate);
System.out.println("受影響的行數(shù): " + rows);
3. 全局配置允許更新空值
如果項(xiàng)目中多處需要更新空值,可以在 Mybatis-Plus 的全局配置中開啟 updateStrategy,允許字段更新為 null。
mybatis-plus:
global-config:
db-config:
update-strategy: not_null # 默認(rèn)值,可以設(shè)置為 'ignore' 以允許更新 null
或者在代碼中進(jìn)行配置:
import com.baomidou.mybatisplus.annotation.DbConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 其他攔截器配置
return interceptor;
}
@Bean
public DbConfig dbConfig() {
return new DbConfig();
}
}
注意:全局配置會(huì)影響所有的更新操作,需謹(jǐn)慎使用。
4. 使用UpdateStrategy注解
在實(shí)體類的字段上使用 @TableField 注解,并設(shè)置 updateStrategy 為 FieldStrategy.IGNORED,以允許該字段在更新時(shí)接受 null 值。
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
public class User {
private Long id;
@TableField(updateStrategy = FieldStrategy.IGNORED)
private String name; // 允許更新為 null
private Integer age;
// getters and setters
}
5. 檢查實(shí)體類的屬性和數(shù)據(jù)庫(kù)字段映射
確保實(shí)體類中的屬性名稱與數(shù)據(jù)庫(kù)表中的字段名稱一致,且類型匹配。
如果存在不一致,可能導(dǎo)致更新無(wú)效。
6. 確認(rèn)事務(wù)是否生效
如果在一個(gè)事務(wù)中進(jìn)行更新操作,確保事務(wù)已正確提交。
未提交的事務(wù)不會(huì)對(duì)數(shù)據(jù)庫(kù)產(chǎn)生影響。
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
@Transactional
public void updateUser(User user) {
userMapper.updateById(user);
}
}
總結(jié)
updateById 方法默認(rèn)不更新空值是為了防止誤操作。
如果確實(shí)需要更新空值,可以通過以下幾種方式實(shí)現(xiàn):
- 使用
UpdateWrapper或LambdaUpdateWrapper并顯式設(shè)置需要更新的字段為null。 - 在全局配置中調(diào)整更新策略(需謹(jǐn)慎)。
- 在實(shí)體類字段上使用注解
@TableField并設(shè)置updateStrategy為IGNORED。 - 確認(rèn)實(shí)體類與數(shù)據(jù)庫(kù)字段的映射關(guān)系以及事務(wù)的正確性。
根據(jù)具體的業(yè)務(wù)需求選擇合適的方法,以確保數(shù)據(jù)更新操作符合預(yù)期。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java編寫計(jì)算器的常見方法實(shí)例總結(jié)
這篇文章主要介紹了Java編寫計(jì)算器的常見方法,結(jié)合實(shí)例形式總結(jié)分析了Java實(shí)現(xiàn)計(jì)算器功能的常用方法,需要的朋友可以參考下2016-04-04
Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用
今天小編就為大家分享一篇關(guān)于Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備(測(cè)試代碼)
BlueCove是一個(gè)開源的藍(lán)牙協(xié)議棧實(shí)現(xiàn),旨在為Java開發(fā)者提供一個(gè)全面的、易于使用的API,從而在應(yīng)用程序中實(shí)現(xiàn)藍(lán)牙功能,該項(xiàng)目支持多種操作系統(tǒng),這篇文章主要介紹了Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備,需要的朋友可以參考下2025-01-01
如何在IDEA Maven項(xiàng)目中導(dǎo)入本地jar包的步驟
今天小編就為大家分享一篇關(guān)于IDEA Maven項(xiàng)目中導(dǎo)入本地jar包的步驟,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
記一次線上SpringCloud Feign請(qǐng)求服務(wù)超時(shí)異常排查問題
這篇文章主要介紹了記一次線上SpringCloud Feign請(qǐng)求服務(wù)超時(shí)異常排查問題,本項(xiàng)目與下游項(xiàng)目均注冊(cè)在Eureka上面,對(duì)這個(gè)1秒就超時(shí)感到很迷惑,于是開始查閱底層源碼之旅。需要的朋友可以參考下2022-01-01
全網(wǎng)最新Log4j?漏洞修復(fù)和臨時(shí)補(bǔ)救方法
Apache?Log4j?遠(yuǎn)程代碼執(zhí)行漏洞,如何快速修復(fù)log4j2漏洞,本文給大家介紹下Log4j?漏洞修復(fù)和臨時(shí)補(bǔ)救方法,感興趣的朋友跟隨小編一起看看吧2021-12-12

