Mybatis-Plus updateById方法更新無效及空值處理過程
在使用 Mybatis-Plus 進(jìn)行數(shù)據(jù)持久化操作時(shí),updateById 方法默認(rèn)不會(huì)更新字段的空值(null)。
這是因?yàn)?Mybatis-Plus 為了防止誤操作,避免將數(shù)據(jù)庫中原本存在的非空字段更新為 null。然而,在某些業(yè)務(wù)場(chǎng)景下,我們可能需要允許更新空值。
以下是幾種解決 updateById 方法不更新空值或更新字段無效問題的方法:
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ù)庫字段映射
確保實(shí)體類中的屬性名稱與數(shù)據(jù)庫表中的字段名稱一致,且類型匹配。
如果存在不一致,可能導(dǎo)致更新無效。
6. 確認(rèn)事務(wù)是否生效
如果在一個(gè)事務(wù)中進(jìn)行更新操作,確保事務(wù)已正確提交。
未提交的事務(wù)不會(huì)對(duì)數(shù)據(jù)庫產(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ù)庫字段的映射關(guān)系以及事務(wù)的正確性。
根據(jù)具體的業(yè)務(wù)需求選擇合適的方法,以確保數(shù)據(jù)更新操作符合預(yù)期。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中整合JodConverter實(shí)現(xiàn)文件在線預(yù)覽功能
Spring Boot JodConverter是一個(gè)基于Spring Boot框架的文檔轉(zhuǎn)換工具,它使用JodConverter庫來實(shí)現(xiàn)文檔格式之間的轉(zhuǎn)換,本文主要介紹了SpringBoot中整合JodConverter實(shí)現(xiàn)文件在線預(yù)覽功能,需要的朋友可以參考下2024-04-04
如何通過Java實(shí)現(xiàn)時(shí)間軸過程解析
這篇文章主要介紹了如何通過Java實(shí)現(xiàn)時(shí)間軸過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Spring如何實(shí)現(xiàn)輸出帶動(dòng)態(tài)標(biāo)簽的日志
文章介紹了如何通過動(dòng)態(tài)標(biāo)簽日志實(shí)現(xiàn),解決了部分業(yè)務(wù)代碼在多個(gè)模塊中調(diào)用時(shí)日志無法直觀看出來源的問題,主要通過ThreadLocal存儲(chǔ)業(yè)務(wù)標(biāo)簽,并在日志輸出時(shí)插入該標(biāo)簽,實(shí)現(xiàn)日志的動(dòng)態(tài)標(biāo)簽功能,感興趣的朋友一起看看吧2024-12-12
解決執(zhí)行maven命令時(shí)提示Process terminated的問題
這篇文章主要介紹了解決執(zhí)行maven命令時(shí)提示Process terminated的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Spring實(shí)戰(zhàn)之@Autowire注解用法詳解
這篇文章主要介紹了Spring實(shí)戰(zhàn)之@Autowire注解用法,結(jié)合實(shí)例形式詳細(xì)分析了Spring @Autowire注解具體實(shí)現(xiàn)步驟與相關(guān)使用技巧,需要的朋友可以參考下2019-12-12
Java?pdf文件書簽承前縮放驗(yàn)證的設(shè)置方法
很多朋友不知道是什么是書簽承前縮放,簡單說就是可以任意改變當(dāng)前pdf文檔縮放比例,點(diǎn)擊書簽后不影響其縮放比率,本文給大家介紹下Java?pdf文件書簽承前縮放驗(yàn)證的設(shè)置方法,感興趣的朋友一起看看吧2022-02-02

