相关文章推荐
侠义非凡的椅子  ·  pandas update mysql - ...·  1 周前    · 
发怒的棒棒糖  ·  避免SQL Server ...·  3 天前    · 
不爱学习的甘蔗  ·  首届CCF ...·  1 年前    · 
低调的菠菜  ·  docker stack not ...·  1 年前    · 
会开车的罐头  ·  bootstrap-datetimepick ...·  1 年前    · 

开发项目中,使用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()));

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

因为最近在忙项目,好久都没有 更新 博客,最近在项目中刚好遇到一个问题,就是在 使用 MyBatis-Plus update ById (xxx)的时候,居然 更新 不了字符串或者 null ,本文分享两种解决方案,具体大家可以根据自己的需求选择一种方法解决。 在实际项目中,难免 更新 的时候,有可能会把已有的值 更新 成空字符串或者 null ,但是当你 使用 update ById ()方法的时候,会发现根...
1. Mybatis Plus 的 FieldStrategy 有三种策略2. 配置3. 使用 update () 方法 1. Mybatis Plus 的 FieldStrategy 有三种策略 IGNORED:忽略。当策略为 IGNORED 时,表示忽略该 字段 的空值判断,即无论实体对象的值为空还是非空,都会进行新增、 更新 操作。 NOT_ NULL :非 NULL 。当策略为 NOT_ NULL 时,表示 字段 不能为空,如果实体对象的值为空,则不会进行新增、 更新 操作。 NOT_EMPTY:非空。当策略为 NOT_
由于 Mybatis plus默认的 更新 策略是NOT_ NULL :非 NULL ;即通过接口 更新 数据时数据为 NULL 值时将不 更新 进数据库。所以 Mybatis plus通过 update ById (XXX) 更新 数据,当用户有 更新 字段 为 空字符串 或者 null 的需求时,需要对 FieldStrategy 策略进行调整。 FieldStrategy 有三种策略: IGNORED:0 忽略 NOT_ NULL :1 非 NULL ,默认策略 NOT_EMPTY:2 非空 方式一:调整全局的验证策略 注入配置 ..
mybatis-plus 中的 update ById 方法,正常情况下,如果设置 字段 null ,由于默认的 字段 策略,不会操作此 字段 ,不会变为 null 值,那应该怎么做的? 第一种: 不推荐 上面我说了,默认的 字段 策略会忽略掉,所以我们可以将要设置为 null 字段 的策略更改下.例如; @TableField(strategy = FieldStrategy.IGNORED) private LocalD...
使用 Mybatis-plus update ById ()方法来 更新 一条记录时:只会 更新 不为 null 字段 ,为 null 字段 会不变。在真实的线上环境是这样的。 所以要是该方法,建议:new一个新的实体,将id赋值,然后把需要 更新 字段 set下。 User user = new User(); user.setId(19); //查询id的条件 user.setRole(" update Test"); // 更新 字段 的值 System.out.println(userDao.updat
一、 update ById 根据主键id 更新 ,传啥改啥!!! 对未修改的无影响 int update ById (@Param(Constants.ENTITY) T entity); 二、 update 根据 whereWrapper 条件 更新 记录,传啥改啥!!! 可用于批量 更新 // 根据 whereWrapper 条件, 更新 记录 int update (@Param(Constants.ENTITY) T update Entity, @Param(Constants.WRAPPER) Wrapper wh