================================
©Copyright 蕃薯耀 2022-06-25
https://www.cnblogs.com/fanshuyao/
一、問題描述
使用這兩個方法,不會對實體中值為Null的屬性(字段)進行更新。
this.updateById(entity);
this.update(entity, updateWrapper);
二、解決方案
1、使用LambdaUpdateWrapper(推薦)
LambdaUpdateWrapper<BizFile> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//過濾條件
lambdaUpdateWrapper.eq(BizFile::getId, bizFile.getId());
//下面為設定值
//由于parentId會為空,是以要使用LambdaUpdateWrapper
lambdaUpdateWrapper.set(BizFile::getParentId, parentId);
lambdaUpdateWrapper.set(BizFile::getPath, newDirPath);
//更新
this.update(lambdaUpdateWrapper);
2、使用UpdateWrapper
和LambdaUpdateWrapper的差別,就是設定的字段寫法不一樣,下面是要使用資料庫字段的,如果修改字段後,容易造成字段名稱沒有修改。
UpdateWrapper<BizFile> updateWrapper = new UpdateWrapper<BizFile>();
updateWrapper.eq("id", bizFile.getId());
updateWrapper.set("parentId", parentId);
updateWrapper.set("path", newDirPath);
this.update(updateWrapper);
3、在實體中使用@TableField注解
在字段上加上注解:@TableField(fill = FieldFill.UPDATE)
@ApiModelProperty("父ID")
@TableField(fill = FieldFill.UPDATE)
private Long parentId;
然後通過下面的方法更新
this.updateById(entity);
this.update(entity, updateWrapper);
(時間寶貴,分享不易,捐贈回饋,^_^)
================================
©Copyright 蕃薯耀 2022-06-25
https://www.cnblogs.com/fanshuyao/
今天越懶,明天要做的事越多。