欢迎来到徐庆高(Tea)的个人博客网站
磨难很爱我,一度将我连根拔起。从惊慌失措到心力交瘁,我孤身一人,但并不孤独无依。依赖那些依赖我的人,信任那些信任我的人,帮助那些给予我帮助的人。如果我愿意,可以分裂成无数面镜子,让他们看见我,就像看见自己。察言观色和模仿学习是我的领域。像每个深受创伤的人那样,最终,我学会了随遇而安。
当前位置: 日志文章 > 详细内容

Mybatis-Plus updateById方法更新无效及空值处理过程

2025年07月15日 Java
在使用 mybatis-plus 进行数据持久化操作时,updatebyid 方法默认不会更新字段的空值(null)。这是因为 mybatis-plus 为了防止误操作,避免将数据库中原本存在的非空字

在使用 mybatis-plus 进行数据持久化操作时,updatebyid 方法默认不会更新字段的空值(null)。

这是因为 mybatis-plus 为了防止误操作,避免将数据库中原本存在的非空字段更新为 null。然而,在某些业务场景下,我们可能需要允许更新空值。

以下是几种解决 updatebyid 方法不更新空值或更新字段无效问题的方法:

1. 使用updatewrapper并设置setsqlselect

通过 updatewrapper 可以灵活地控制更新的字段,包括允许更新为 null

import com.baomidou.mybatisplus.core.conditions.update.updatewrapper;

// 假设有一个实体类 user
user user = new user();
user.setid(1); // 需要更新的记录id
user.setname(null); // 需要更新为空的字段
user.setage(30);

updatewrapper<user> updatewrapper = new updatewrapper<>();
updatewrapper.eq("id", user.getid())
             .set("name", user.getname()) // 允许 name 字段更新为 null
             .set("age", user.getage());

int rows = usermapper.update(null, updatewrapper);
system.out.println("受影响的行数: " + rows);

2. 使用lambdaupdatewrapper并调用set方法

lambdaupdatewrapper 提供了类型安全的更新方式,同样可以设置字段为 null

import com.baomidou.mybatisplus.core.conditions.update.lambdaupdatewrapper;

user user = new user();
user.setid(1);
user.setname(null); // 需要更新为空的字段
user.setage(30);

lambdaupdatewrapper<user> lambdaupdate = new lambdaupdatewrapper<>();
lambdaupdate.eq(user::getid, user.getid())
           .set(user::getname, user.getname()) // 允许 name 字段更新为 null
           .set(user::getage, user.getage());

int rows = usermapper.update(null, lambdaupdate);
system.out.println("受影响的行数: " + rows);

3. 全局配置允许更新空值

如果项目中多处需要更新空值,可以在 mybatis-plus 的全局配置中开启 updatestrategy,允许字段更新为 null

mybatis-plus:
  global-config:
    db-config:
      update-strategy: not_null # 默认值,可以设置为 'ignore' 以允许更新 null

或者在代码中进行配置:

import com.baomidou.mybatisplus.annotation.dbconfig;
import com.baomidou.mybatisplus.extension.plugins.mybatisplusinterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.paginationinnerinterceptor;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
public class mybatisplusconfig {

    @bean
    public mybatisplusinterceptor mybatisplusinterceptor() {
        mybatisplusinterceptor interceptor = new mybatisplusinterceptor();
        // 其他拦截器配置
        return interceptor;
    }

    @bean
    public dbconfig dbconfig() {
        return new dbconfig();
    }
}

注意:全局配置会影响所有的更新操作,需谨慎使用。

4. 使用updatestrategy注解

在实体类的字段上使用 @tablefield 注解,并设置 updatestrategyfieldstrategy.ignored,以允许该字段在更新时接受 null 值。

import com.baomidou.mybatisplus.annotation.fieldstrategy;
import com.baomidou.mybatisplus.annotation.tablefield;

public class user {
    
    private long id;
    
    @tablefield(updatestrategy = fieldstrategy.ignored)
    private string name; // 允许更新为 null
    
    private integer age;

    // getters and setters
}

5. 检查实体类的属性和数据库字段映射

确保实体类中的属性名称与数据库表中的字段名称一致,且类型匹配。

如果存在不一致,可能导致更新无效。

6. 确认事务是否生效

如果在一个事务中进行更新操作,确保事务已正确提交。

未提交的事务不会对数据库产生影响。

import org.springframework.transaction.annotation.transactional;

@service
public class userservice {

    @transactional
    public void updateuser(user user) {
        usermapper.updatebyid(user);
    }
}

总结

updatebyid 方法默认不更新空值是为了防止误操作。

如果确实需要更新空值,可以通过以下几种方式实现:

  1. 使用 updatewrapperlambdaupdatewrapper 并显式设置需要更新的字段为 null
  2. 在全局配置中调整更新策略(需谨慎)。
  3. 在实体类字段上使用注解 @tablefield 并设置 updatestrategyignored
  4. 确认实体类与数据库字段的映射关系以及事务的正确性。

根据具体的业务需求选择合适的方法,以确保数据更新操作符合预期。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。