| 符号 | 原符号 | 替换符号 |
|---|---|---|
| 小于 | < | < |
| 小于等于 | <= | <= |
| 大于 | > | > |
| 大于等于 | >= | >= |
| 不等于 | <> | <> |
| 与 | & | & |
| 单引号 | ’ | ' |
| 双引号 | " | " |
或者使用 CDATA
<![CDATA[ 转义语句 ]]>
<![CDATA[spd.date >= #{beginDate}]]>
if-else判断语句
<choose>
<when test="startIndex != null">
limit #{startIndex}
</when>
<otherwise>
limit 0
</otherwise>
</choose>
MyBatis-Plus 更新空字段
一、问题描述
使用这两个方法,不会对实体中值为Null的属性(字段)进行更新。
this.updateById(entity);this.update(entity, updateWrapper);
二、解决方案
1、使用LambdaUpdateWrapper (推荐)
LambdaUpdateWrapper<BizFile> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//过滤条件
lambdaUpdateWrapper.eq(BizFile::getId, bizFile.getId());
//下面为设置值
//由于parentId会为空,所以要使用
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);

本文介绍使用MyBatis-Plus时如何解决实体中值为Null的属性不被更新的问题。提供了三种方法:使用LambdaUpdateWrapper、UpdateWrapper及实体注解@TableField。这些方法确保了即使是空值也能被正确更新。
3203

被折叠的 条评论
为什么被折叠?



