Mybatis-Plus更新表字段为null

问题

业务上需要将一张表的一个字段从有值置为null,使用mybatis-plus(版本号为3.3.0)的updateById() 方法成功但数据库数据未修改,控制台打印的sql也没有对字段belong_type更新

	/**
     * 使用 boolean updateById(T entity);
     * @param typeId
     * @return
     */
    public boolean removeType(Integer typeId) {
        ChannelType channelType = getById(typeId);
        channelType.setBelongType(null);
        return updateById(channelType);
    }
UPDATE channel_type SET type_name=?, type_code=?, is_received=?, is_system_type=?, data_source=?, create_time=? WHERE id=? AND delete_flag=0 
Parameters: 2(String), 2(String), 1(Integer), 0(Integer), 2(String), 2021-03-23 00:00:00.0(Timestamp), 25(Integer)

分析

mybatis-plus在更新操作的时候,对null值的设置默认不采用。
具体原因:mybatis-plus中有一个枚举类FieldStrategy 如下

package com.baomidou.mybatisplus.annotation;

public enum FieldStrategy {
    IGNORED,
    NOT_NULL,
    NOT_EMPTY,
    DEFAULT,
    NEVER;

    private FieldStrategy() {
    }
}

mybatis-plus默认更新策略是NOT_NULL,这个默认策略导致了更新null值失效。

解决方案

1. 修改配置

并不推荐修改配置,可能会对其他业务造成影响,所以推荐针对当前业务逻辑进行更新

2. 使用UpdateWrapper方式更新

方法修改:

    /**
     * 使用 UpdateWrapper
     * @param typeId
     * @return
     */
    public boolean removeType(Integer typeId) {
        UpdateWrapper uw = new UpdateWrapper();
        uw.set(DBConstants.BELONG_TYPE, null);
        uw.eq(DBConstants.ID, typeId);
        return update(uw);
    }

执行sql:

Preparing: UPDATE channel_type SET belong_type=? WHERE delete_flag=0 AND (id = ?) 
==> Parameters: null, 25(Integer)

IService层方法:

public interface IService<T>{
	...
	boolean update(T entity, Wrapper<T> updateWrapper);

    default boolean update(Wrapper<T> updateWrapper) {
        return update(null, updateWrapper);
    }
}    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Mybatis-Plus进行DTO的操作可以通过以下步骤实现: 1. 首先,确保已经引入了Mybatis-Plus的依赖,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 2. 创建DTO(Data Transfer Object)对象,用于封装查询结果。DTO对象通常包含与数据库字段对应的属性,并提供相应的getter和setter方法。 3. 在Mapper接口中使用@Mapper注解标识该接口为Mybatis的Mapper接口,并使用@MapperScan注解扫描Mapper接口所在的包。 4. 在Mapper接口中定义查询方法,可以使用Mybatis-Plus提供的查询方法,也可以自定义SQL语句进行查询。在查询方法的参数中使用@Param注解指定参数名称,以便在SQL语句中引用。 5. 在Service层中调用Mapper接口的查询方法,获取查询结果。 6. 将查询结果封装到DTO对象中,并返回给前端或其他需要的地方。 总结起来,使用Mybatis-Plus进行DTO的操作主要包括创建DTO对象、定义Mapper接口的查询方法、在Service层调用查询方法并封装结果到DTO对象中。这样可以实现使用Mybatis-Plus进行DTO的操作。 #### 引用[.reference_title] - *1* *3* [Mybatis-plus做连接查询的插件Mybatis-plus-join](https://blog.csdn.net/m0_67400973/article/details/126463252)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Mybatis-Plus 开发提速器:mybatis-plus-generator-ui 你确定不了解一下?](https://blog.csdn.net/weixin_44421461/article/details/129483755)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值