栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis-Plus 字段为Null时不更新解决方案,MyBatis-Plus 更新空字段

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis-Plus 字段为Null时不更新解决方案,MyBatis-Plus 更新空字段

================================

©Copyright 蕃薯耀 2022-06-25

https://www.cnblogs.com/fanshuyao/

一、问题描述

使用这两个方法,不会对实体中值为Null的属性(字段)进行更新。

this.updateById(entity);

this.update(entity, updateWrapper);

二、解决方案

1、使用LambdaUpdateWrapper(推荐)
LambdaUpdateWrapper 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 updateWrapper = new UpdateWrapper();
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/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993129.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号