Mybatis-plus:update的方法

本文展示了三种在Java中使用SpringBoot框架更新数据库记录的方法:1)通过主键id修改User对象;2)利用UpdateWrapper根据name属性查询并更新age;3)使用LambdaUpdateWrapper设置查询和修改条件更新name。

方法1:通过主键id进行修改

    @Test
    void testUpdateById() {
        User user = new User();
        user.setId(1L);
        user.setName("牛詩涵");
        user.setPassword("12345");
        userRepository.updateById(user);
        System.out.println("update success....");
    }

方法2:通过实体类Wrapper查询进行修改

    @Test
    void testUserUpdateWrapper() {
        //作为查询条件
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("name", "牛詩涵");
        //new出来的对象作为修改值
        User user = new User();
        user.setAge(23);
        //直接update修改
        userRepository.update(user, userUpdateWrapper);
        System.out.println("update success...");
    }

方法3:LambdaUpdateWrapper进行修改

    @Test
    void testLambUpdate(){
        LambdaUpdateWrapper<User> ulqw = new LambdaUpdateWrapper<>();
        //eq是要查询的条件,set是要修改后的值
        ulqw.eq(User::getName,"Luis Ramos");
        ulqw.set(User::getName,"牛詩涵");
        userRepository.update(null,ulqw);
    }

在使用 **MyBatis-Plus** 时,如果你希望只更新实体中的某些**指定字段**,而不是将整个实体的所有字段都更新到数据库(避免 `null` 值覆盖),可以通过以下几种方式实现 **只 update 指定字段**。 --- ### ✅ 方法一:使用 `UpdateWrapper` + `set()` 显式指定字段(推荐) 这是最灵活、最安全的方式,**不依赖实体对象的属性值**,可以精确控制要更新哪些字段。 ```java LambdaUpdateWrapper<IoStatistics> wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(IoStatistics::getId, 123L) .set(IoStatistics::getReportDate, LocalDateTime.now()) .set(IoStatistics::getStatus, 1); // 执行更新 ioStatisticsMapper.update(null, wrapper); ``` #### 🔍 说明: - `update(null, wrapper)`:第一个参数传 `null` 表示不使用实体对象的数据。 - `.set(...)`:手动设置某个字段的值。 - 生成 SQL 示例: ```sql UPDATE io_statistics SET report_date = ?, status = ? WHERE id = 123; ``` > ⭐ 完全避免了 `null` 字段被更新的问题,推荐用于精确更新场景。 --- ### ✅ 方法二:使用 `UpdateWrapper` 结合实体对象(部分字段赋值) 先给实体对象设置你要更新的字段,其他字段保持为 `null` 或默认值,然后配合 `UpdateWrapper` 使用。 ```java IoStatistics statistics = new IoStatistics(); statistics.setId(123L); // 主键用于 where 条件 statistics.setReportDate(LocalDateTime.now()); statistics.setStatus(1); LambdaUpdateWrapper<IoStatistics> wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(IoStatistics::getId, statistics.getId()); ioStatisticsMapper.update(statistics, wrapper); ``` > ⚠️ 注意:这种方式要求 MyBatis-Plus 的 **字段策略** 支持忽略 `null` 值。否则 `null` 字段可能会被更新成 `NULL`。 --- ### ✅ 方法三:配置字段更新策略(全局或字段级) 确保 `null` 字段不会参与更新,通过配置字段填充策略。 #### 方式 1:全局配置(application.yml) ```yaml mybatis-plus: global-config: db-config: field-strategy: NOT_NULL # INSERT 和 UPDATE 都不更新 null 字段(已废弃) # 新版本使用 insertStrategy / updateStrategy insert-strategy: NOT_NULL update-strategy: NOT_NULL ``` > ⚠️ 注意:`field-strategy` 在 **MyBatis-Plus 3.3.0+ 已废弃**,应使用: ```yaml mybatis-plus: configuration: # 全局配置字段更新策略 global-config: db-config: update-strategy: NOT_NULL ``` #### 方式 2:字段级别注解(更精细控制) ```java public class IoStatistics { private Long id; @TableField(updateStrategy = FieldStrategy.NOT_NULL) private LocalDateTime reportDate; @TableField(updateStrategy = FieldStrategy.NOT_NULL) private Integer status; } ``` 这样即使你传入了实体对象,只要字段是 `null`,就不会出现在 `UPDATE` 语句中。 --- ### ✅ 方法四:使用 `UpdateChainWrapper`(链式调用) 适合简单场景,代码更简洁: ```java boolean success = ioStatisticsService.lambdaUpdate() .eq(IoStatistics::getId, 123L) .set(IoStatistics::getReportDate, LocalDateTime.now()) .set(IoStatistics::getStatus, 2) .update(); if (success) { System.out.println("更新成功"); } ``` --- ### ❌ 错误做法(容易出问题) ```java IoStatistics entity = new IoStatistics(); entity.setId(123L); entity.setReportDate(null); // 这个 null 可能会把数据库字段置为 NULL! ioStatisticsMapper.updateById(entity); ``` > 如果没有正确配置 `updateStrategy`,这会导致 `report_date = NULL` 被执行! --- ### ✅ 推荐最佳实践总结 | 场景 | 推荐方法 | |------|----------| | 只更新几个字段,且不想动实体 | ✅ `LambdaUpdateWrapper` + `.set()` | | 更新一个实体的部分非空字段 | ✅ 实体 + `updateStrategy = NOT_NULL` | | 链式编程风格 | ✅ `lambdaUpdate().eq().set().update()` | | 批量更新指定字段 | ✅ `service.update(batchEntityList, updateWrapper)` | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值