Mybatis-Plus更新數(shù)據(jù)忽略null值問題
一、問題描述
近期正在家里美美休假,突然接個(gè)電話說是業(yè)務(wù)反饋生產(chǎn)上要操作置空某個(gè)字段,操作之后返回信息是成功,但實(shí)際沒有反應(yīng)。于是立刻打開電腦看看什么情況。
public Result<CstGrpInfoDto> edit(@Valid @RequestBody CstGrpInfoDto cstGrpInfoDto) {
Result<CstGrpInfoDto> result = new Result<CstGrpInfoDto>();
CstGrpInfo cstGrpInfoEo = cstGrpInfoService.getById(cstGrpInfoDto.getId());
if (cstGrpInfoEo == null) {
result.error400("未找到對(duì)應(yīng)實(shí)體");
} else {
BeanUtils.copyProperties(cstGrpInfoDto, cstGrpInfoEo);
try {
cstGrpInfoMapper.updateById(cstGrpInfoEo);
result.success("修改成功!");
} catch (Exception e) {
log.error(e.getMessage(), e);
result.error400("修改失敗!");
}
}
return result;
}大致邏輯就是這樣,很簡(jiǎn)單的代碼。
一開始想難道是BeanUtils的鍋嗎?于是DEBUG查看,發(fā)現(xiàn)EO的屬性值都沒有問題,問題只能是在mybatisPlus的updateById方法上。
通過調(diào)用查看日志,發(fā)現(xiàn)調(diào)用updateById方法拼接的SQL中,不包含屬性值為null部分的字段.于是推測(cè):mybatisPlus不會(huì)修改為null的字段,上網(wǎng)一搜果然是這樣。
二、解決方案
方法1、使用UpdateWrapper方式更新
該方法適合明確需要修改的字段。
示例:
public int updateProduct(String productCode) {
UpdateWrapper<Product> wrapper = new UpdateWrapper<>();
wrapper.lambda().eq(Product::getProductCode, productCode)
.set(Product::getName, null);
return getBaseMapper().update(null, wrapper);
}方法2、對(duì)某個(gè)字段設(shè)置單獨(dú)的field-strategy
根據(jù)具體情況,在需要更新的字段中調(diào)整驗(yàn)證注解,如下
@TableField(strategy = FieldStrategy.IGNORED) private String name;
方法3、設(shè)置全局的field-strategy (慎用)
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加分頁(yè)插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
// 設(shè)置全局的更新策略
GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
dbConfig.setUpdateStrategy(FieldStrategy.IGNORED); // 設(shè)置為忽略 null 值
globalConfig.setDbConfig(dbConfig);
return globalConfig;
}
}properties文件格式:
mybatis-plus.global-config.db-config.update-strategy=ignored
yml文件格式:
mybatis-plus:
global-config:
db-config:
update-strategy: ignored # 設(shè)置為忽略 null 值使用建議:建議根據(jù)業(yè)務(wù)實(shí)際情況來選擇使用哪種方式,對(duì)于有默認(rèn)值和不可為null的數(shù)據(jù),建議使用默認(rèn)策略。
到此這篇關(guān)于Mybatis-Plus更新數(shù)據(jù)忽略null值問題的文章就介紹到這了,更多相關(guān)Mybatis-Plus更新數(shù)據(jù)忽略null值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven實(shí)現(xiàn)項(xiàng)目構(gòu)建工具
本文主要介紹了Maven實(shí)現(xiàn)項(xiàng)目構(gòu)建工具,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼
這篇文章主要介紹了Spring Security OAuth2實(shí)現(xiàn)登錄互踢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Spring之SseEmitter實(shí)現(xiàn)讓你的進(jìn)度條實(shí)時(shí)更新
Spring SseEmitter是一種實(shí)現(xiàn)服務(wù)器端推送事件(SSE)的機(jī)制,支持單向通信,適用于實(shí)時(shí)數(shù)據(jù)傳輸需求,通過代碼示例和應(yīng)用場(chǎng)景分析,展示了如何在服務(wù)端和客戶端使用SseEmitter進(jìn)行實(shí)時(shí)數(shù)據(jù)推送2025-02-02

