当前位置: 首页 > news >正文

saveOrUpdate 有个缺点,不会把值赋值为null,解决办法

针对 MyBatis-Plus 的 saveOrUpdate 方法无法将字段更新为 null 的问题,这是因为 MyBatis-Plus 默认会忽略 null 值字段。以下是几种解决方案:

方案 1:使用 update(entity, wrapper) 手动指定更新条件

原理:通过 UpdateWrapper 明确指定需要更新为 null 的字段。

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {public void updateWithNullValue() {User user = new User();user.setId(1L);user.setUsername(null); // 需要更新为 null 的字段// 手动构建 UpdateWrapper,强制更新 null 值字段UpdateWrapper<User> wrapper = new UpdateWrapper<>();wrapper.set("username", null) // 指定 username 字段更新为 null.eq("id", user.getId()); // 更新条件:ID = 1// 调用 update 方法,第一个参数可以为 null(因为条件已在 wrapper 中)boolean success = this.update(null, wrapper);if (success) {System.out.println("更新 null 值成功");}}
}

方案 2:配置全局字段策略(推荐)

原理:通过配置 FieldStrategy 让 MyBatis-Plus 不忽略 null 值。

方式 1:配置文件(application.yml)
mybatis-plus:global-config:db-config:update-strategy: IGNORED  # 更新策略:忽略判断,所有字段都更新(包括 null)
方式 2:Java 配置类
@Configuration
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 其他配置...// 配置全局策略GlobalConfig globalConfig = new GlobalConfig();GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();dbConfig.setUpdateStrategy(FieldStrategy.IGNORED); // 更新时忽略字段非空判断globalConfig.setDbConfig(dbConfig);return interceptor;}
}

方案 3:使用 @TableField 注解(细粒度控制)

原理:在实体类字段上指定更新策略为 IGNORED

@Data
@TableName("t_user")
public class User {@TableIdprivate Long id;@TableField(updateStrategy = FieldStrategy.IGNORED) // 该字段更新时忽略 null 判断private String username;private String phone;@TableField(updateStrategy = FieldStrategy.IGNORED) // 该字段更新时忽略 null 判断private Integer age;
}

方案 4:自定义 SQL 方法

原理:在 Mapper 接口中自定义更新方法,使用 @Update 注解编写 SQL。

public interface UserMapper extends BaseMapper<User> {@Update("UPDATE t_user SET username = #{username}, age = #{age} WHERE id = #{id}")boolean updateWithNull(@Param("id") Long id, @Param("username") String username, @Param("age") Integer age);
}

Service 层调用

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {public void customUpdateWithNull() {boolean success = baseMapper.updateWithNull(1L, null, 25);if (success) {System.out.println("自定义 SQL 更新 null 值成功");}}
}

总结

方案优点缺点适用场景
方案 1灵活,按需更新代码冗余少量需要更新 null 的场景
方案 2全局生效,无需修改代码影响所有实体项目中大量需要更新 null
方案 3细粒度控制,不影响其他字段需修改实体类部分字段需要更新 null
方案 4完全自定义 SQL维护成本高复杂更新逻辑或性能敏感场景

推荐顺序:方案 3(细粒度控制)→ 方案 2(全局配置)→ 方案 1(临时需求)→ 方案 4(特殊场景)。


文章转载自:

http://iHmokbNb.zxdhp.cn
http://Ru9GSJ2a.zxdhp.cn
http://8wJa8CQN.zxdhp.cn
http://y3GqkRzY.zxdhp.cn
http://QfOHdYxP.zxdhp.cn
http://4PP8ablq.zxdhp.cn
http://wZkghMwa.zxdhp.cn
http://x6ap4lWy.zxdhp.cn
http://dTLib25r.zxdhp.cn
http://T78qemZK.zxdhp.cn
http://OmUVq3v5.zxdhp.cn
http://fEZPC5F1.zxdhp.cn
http://mwVncqZZ.zxdhp.cn
http://Nfe8o7CS.zxdhp.cn
http://Dxc7a4oy.zxdhp.cn
http://jOduaquo.zxdhp.cn
http://Wkrm1e0S.zxdhp.cn
http://B3HJl5pW.zxdhp.cn
http://75npmyqE.zxdhp.cn
http://A68Ul2fa.zxdhp.cn
http://FXs9YWiM.zxdhp.cn
http://DfW0rPUE.zxdhp.cn
http://osoAWuTn.zxdhp.cn
http://Wm9vHpd0.zxdhp.cn
http://OzwTQ9lT.zxdhp.cn
http://xu5mmCy9.zxdhp.cn
http://yhVTRFtX.zxdhp.cn
http://qOY1VV7M.zxdhp.cn
http://qWGkZnDs.zxdhp.cn
http://FpC6tKud.zxdhp.cn
http://www.dtcms.com/a/228821.html

相关文章:

  • 笔记︱数据科学领域因果推断案例集锦(第三弹)
  • 爱普生Epson L3210打印机信息
  • LabVIEW磁悬浮轴承传感器故障识别
  • 金融中的线性优化:投资组合分配与求解器 - Part 2
  • SpringBoot系列之RabbitMQ 实现订单超时未支付自动关闭功能
  • 【氮化镓】GaN HMETs器件物理失效分析进展
  • 正点原子lwIP协议的学习笔记
  • 关于list集合排序的常见方法
  • 网络爬虫 - App爬虫及代理的使用(十一)
  • CodeTop100 Day21
  • Python微积分可视化:从导数到积分的交互式教学工具
  • 【Typst】1.Typst概述
  • 详解代理型RAG与MCP服务器集成
  • Java中并发修改异常如何处理
  • MaxCompute开发UDF和UDTF案例
  • TDengine 的 AI 应用实战——运维异常检测
  • 软件测试环境搭建与测试流程
  • 新手小白使用VMware创建虚拟机练习Linux
  • C++算法训练营 Day6 哈希表(1)
  • 用AI(Deepseek)做了配色网站-功能介绍【欢迎体验】
  • 人工智能在智能教育中的创新应用与未来趋势
  • “声网AI多语种翻译官:跨境导游的生存革命“
  • Unity UI 性能优化--Sprite 篇
  • Easyui悬停组件
  • 托福39-1 Early Writing Systems感悟
  • 黑客利用GitHub现成工具通过DevOps API发起加密货币挖矿攻击
  • 秋招准备-数据结构
  • 如何安装huaweicloud-sdk-core-3.1.142.jar到本地仓库?
  • Linux 命令全讲解:从基础操作到高级运维的实战指南
  • 滚动部署详解