Mybatis-plus uses this.baseMapper.update and updateById to update the null value and it does not take effect

mybatis-plus can directly use this.baseMapper.update and updateById in the serviceImpl layer.
If the updated field value is null, the field that needs to be updated to null will not be updated successfully. The printed sql also does not update the fields to null.

reason:

Mybatis-Plus默认的更新策略设置的问题,Mybatis-Plus中FieldStrategy有三种策略:
  IGNORED:忽略。不管有没有有设置属性,所有的字段都会设置到insert语句中,如果没设置值会更新为null;
  NOT_NULL:非 NULL,默认策略。也就是忽略null的字段,不忽略"";
  NOT_EMPTY:非空。为null,为空串的忽略,就是如果设置值为null,“”,不会插入数据库;

Solution:
1) When you need to set a field that may be null, you need to use lambdaUpdate()
. The usage method is:

        this.lambdaUpdate()
            .set(对象::get字段名, 需要修改为什么样的值)
            .eq(对象::get条件字段名, 条件满足这个值)
            .update(new 对象());
            
      // 举个例子
            this.lambdaUpdate()
            .set(User::getName(), name)
            .eq(User::getUserId, userId)
            .update(new User());

or

  sysUserService.update(null, new LambdaUpdateWrapper<SysUser>()
                .eq(SysUser::getUsername,username)
                .set(SysUser::getName,null)
                .set(SysUser::getPassword,null)
        );


  1. Add the annotation @TableField(updateStrategy = FieldStrategy.IGNORED) to the fields that need to be updated in the entity class
@TableField(insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)
private String name;

单个的例子:

@TableField(updateStrategy = FieldStrategy.IGNORED)
private String username;

Guess you like

Origin blog.csdn.net/Little_Arya/article/details/129737960