【Mybatis-Plus】使用updateById()、update()将字段更新为null

问题背景

        开发项目中,使用MyBatis-Plus 的updateById() 方法将查询结果中原本不为null的字段更新为null,该字段设置可为null。发现更新失败。

问题原因

 mybatis-plus FieldStrategy 有三种策略:

  • IGNORED:0 忽略
  • NOT_NULL:1 非 NULL,默认策略
  • NOT_EMPTY:2 非空

而默认更新策略是NOT_NULL:非 NULL;即通过接口更新数据时数据为NULL值时将不更新进数据库。

解决方案

1、写sql。直接在xml中写update sql语句。

2、设置全局的FieldStrategy

        在配置文件中修改全局策略

#properties文件格式:
mybatis-plus.global-config.db-config.field-strategy=ignored

#yml文件格式:
mybatis-plus:
  global-config:
      #字段策略 0:"忽略判断",1:"非 NULL 判断",2:"非空判断"
    field-strategy: 0

        这样做是全局配置,会对所有字段忽略判断。如果一些字段没有传值过来,会被直接更新为null,可能会影响其他业务数据的准确性。 不推荐

3、对指定字段单独设置field-strategy

根据具体情况,在需要更新的字段中调整验证注解,如验证非空:
@TableField(strategy=FieldStrategy.NOT_EMPTY)

这样的话,我们只需要在需要更新为null的字段上,设置忽略策略,如下:

/**
 *
 */
@TableField(updateStrategy = FieldStrategy.IGNORED)
private Date patchedDate;

在更新代码中,我们直接使用mybatis-plus中的updateById方法便可以更新成功,如下:

DbPatchScheduleEntity schedule = dbPatchScheduleService.getById(scheduleRequest.getScheduleId());
schedule.setScheduleStatus(DbPatchConstant.ScheduleStatus.UNSCHEDULED);
schedule.setPatchedDate(null);
dbPatchScheduleService.updateById(schedule);

        使用上述方法,如果需要这样处理的字段较多,那么就涉及到给各个字段加注解,这样弊端也就比较明显了。

4、使用UpdateWrapper方式更新

List<DbPatchScheduleRequestEntity> scheduleRequestList = dbPatchScheduleRequestService.list(new QueryWrapper<DbPatchScheduleRequestEntity>().lambda().eq(DbPatchScheduleRequestEntity::getRequestId, requestId));
scheduleRequestList.forEach(scheduleRequest -> {
	dbPatchScheduleRequestService.update(scheduleRequest, new UpdateWrapper<DbPatchScheduleRequestEntity>().lambda()
									.set(DbPatchScheduleRequestEntity::getScheduleStatus, DbPatchConstant.ScheduleStatus.UNSCHEDULED)
									.set(DbPatchScheduleRequestEntity::getPatchedDate, null)
									.eq(DbPatchScheduleRequestEntity::getId, scheduleRequest.getId()));

        这种方式不影响其他方法,不需要修改全局配置,也不需要在字段上单独加注解,所以推荐使用该方式。

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以在Person类中定义属性时,使用C#中的Attribute语法来进行字段验证。具体步骤如下: 1. 定义一个Attribute类,用于表示要验证的字段信息。例如: ```csharp public class RequiredAttribute : Attribute { } public class ValidDateAttribute : Attribute { } ``` 2. 在Person类中定义属性时,在属性前面加上定义的Attribute类。例如: ```csharp public class Person { [Required] public string Name { get; set; } [ValidDate] public DateTime BirthDate { get; set; } } ``` 3. 在代码中使用反射来获取属性信息,并根据属性前面的Attribute类进行相应的验证。例如: ```csharp public static class PersonValidator { public static bool Validate(Person person) { var properties = typeof(Person).GetProperties(); foreach (var property in properties) { var requiredAttribute = property.GetCustomAttribute<RequiredAttribute>(); var validDateAttribute = property.GetCustomAttribute<ValidDateAttribute>(); if (requiredAttribute != null && string.IsNullOrEmpty(property.GetValue(person)?.ToString())) { return false; } if (validDateAttribute != null && !IsValidDate(property.GetValue(person))) { return false; } } return true; } private static bool IsValidDate(object value) { if (value == null || !DateTime.TryParse(value.ToString(), out var date)) { return false; } return date <= DateTime.Now; } } ``` 这样,在使用Person类时,可以调用PersonValidator.Validate方法来进行字段验证。例如: ```csharp var person = new Person { Name = "张三", BirthDate = new DateTime(1990, 1, 1) }; var isValid = PersonValidator.Validate(person); ``` 上述代码中,如果person的Name和BirthDate字段都符合要求,则isValid为true;否则,为false。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值