mybaties plus 更新null值进入数据库

(数据库一定要支持字段为null值)

问题:

假设我现在数据库里有一个值是1,这个字段允许为null。 目前我使用的是的mybaties plus,我希望将这个值更新weinull,如何操作? 提示:如果直接给代码更新语句set一个null值进去,系统会提示更新成功,但是实际上因为set的是null值所以并不会更新成功

今日被人问到的e

在使用 MyBatis-Plus 时,如果你希望将数据库中的某个字段更新为 NULL,但发现直接设置为 null 后并没有生效,这通常是因为 MyBatis-Plus 默认的更新策略会忽略值为 null 的字段。也就是说,默认情况下,MyBatis-Plus 不会将字段更新为 NULL

为了解决这个问题,你可以通过以下几种方式来实现将字段更新为 NULL


方法 1:使用 @TableField 注解修改更新策略

在实体类中,可以为需要更新为 NULL 的字段添加 @TableField 注解,并指定 updateStrategy = FieldStrategy.IGNORED。这样,即使字段值为 null,也会被包含在更新语句中。

示例代码:
java


深色版本

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("your_table_name")
public class YourEntity {

    private Long id; // 主键

    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String yourNullableField; // 允许为 NULL 的字段

    // Getter 和 Setter 方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getYourNullableField() {
        return yourNullableField;
    }

    public void setYourNullableField(String yourNullableField) {
        this.yourNullableField = yourNullableField;
    }
}
更新逻辑:
java


深色版本

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class YourEntityService extends ServiceImpl<YourEntityMapper, YourEntity> {

    public void updateFieldToNull(Long id) {
        YourEntity entity = new YourEntity();
        entity.setId(id); // 设置主键
        entity.setYourNullableField(null); // 将字段设为 null
        
        // 调用 MyBatis-Plus 的 updateById 方法
        this.updateById(entity);
    }
}

此时,更新语句会生成如下 SQL:

sql


深色版本

UPDATE your_table_name SET your_nullable_field = NULL WHERE id = ?;

方法 2:使用 UpdateWrapper 显式设置字段为 NULL

如果不想修改实体类的注解配置,可以通过 UpdateWrapper 来显式地将字段设置为 NULL

示例代码:
java


深色版本

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

@Service
public class YourEntityService extends ServiceImpl<YourEntityMapper, YourEntity> {

    public void updateFieldToNull(Long id) {
        UpdateWrapper<YourEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id) // 指定更新条件
                     .set("your_nullable_field", null); // 将字段设为 null
        
        // 执行更新操作
        this.update(updateWrapper);
    }
}

此时,生成的 SQL 语句类似于:

sql


深色版本

UPDATE your_table_name SET your_nullable_field = NULL WHERE id = ?;

方法 3:全局配置更新策略

如果你希望在整个项目中都允许字段更新为 NULL,可以在 MyBatis-Plus 的全局配置中修改默认的更新策略。

修改全局配置:

application.ymlapplication.properties 文件中添加以下配置:

yaml


深色版本

mybatis-plus:
  global-config:
    db-config:
      update-strategy: ignored # 全局更新策略:忽略 null 值检查

或者通过 Java 配置类设置:

java


深色版本

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setDbConfig(new GlobalConfig.DbConfig().setUpdateStrategy(com.baomidou.mybatisplus.annotation.FieldStrategy.IGNORED));
        factoryBean.setGlobalConfig(globalConfig);

        return factoryBean.getObject();
    }
}

这样,所有字段都会遵循 IGNORED 策略,即使字段值为 null,也会被包含在更新语句中。