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

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,也会被包含在更新语句中。

相关文章:

  • 【AI编程技术爆发:从辅助工具到生产力革命】
  • 博客园账户注册全流程指南(附常见问题)
  • windows安卓子系统wsa隐藏应用列表的安装激活使用
  • OpenHarmony 5.0版本视频硬件编解码适配
  • 零基础开始学习鸿蒙开发-智能家居APP离线版介绍
  • 基于PLC的采摘机械手(论文+源码)
  • 《Vue Router实战教程》12.不同的历史记录模式
  • 《Python星球日记》第22天:NumPy 基础
  • 安科瑞EMS3.0破解工业企业源网荷储一体化“用能三难”问题 帮助企业高效管理
  • 什么是TensorFlow?
  • OpenGL学习笔记(assimp封装、深度测试、模板测试)
  • 全场景赋能|燕千云打造企业级智能运维平台
  • TensorFlow充分并行化使用CPU
  • 国产Linux系统统信安装redis教程步骤
  • 基于SpringBoot的动物救助中心系统(源码+数据库)
  • Zen 5白色装机优选,华硕X870 AYW GAMING WIFI W主板来了!
  • 网工基础 | 常见英文术语注解
  • 新闻推荐系统(springboot+vue+mysql)含万字文档+运行说明文档
  • 参照Spring Boot后端框架实现序列化工具类
  • [特殊字符] 第十一讲 | 空间回归模型实战:SAR / SEM / GWR逐个击破
  • 雅诗兰黛网络营销策划方案/网站的优化公司
  • 建网站要多少钱一台/沈阳seo排名外包
  • 华为云定制建站服务怎么样/泰州百度公司代理商
  • 用户体验设计师/什么叫做优化
  • 做虚拟货币交易网站/五种关键词优化工具
  • 创意网站建设/seo公司外包