Mybatis-plus使用update()/updateById()将字段更新为null或者空值时候不起作用

2年前 (2022) 程序员胖胖胖虎阿
188 0 0

Mybatis-plus使用update()/updateById()将字段更新为null或者空值时候不起作用。
**原因:**mybatis-plus FieldStrategy 有三种策略:
IGNORED:0 忽略
NOT_NULL:1 非 NULL,默认策略
NOT_EMPTY:2 非空

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

解决方法(3种):
1、在配置文件中,设置全局的field-strategy,如下

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

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


2、对字段进行单独设置
@TableField(updateStrategy= FieldStrategy.IGNORED)

3、使用updateWrapper

LambdaUpdateWrapper<xxx> updateWrapper = new LambdaUpdateWrapper<>();
wrapper.eq(xxx::getId, xxx.getId());
wrapper.set(xxx::getOrgUri, xxx.getOrgUri());
wrapper.set(xxx::getBeginTime, null);
xxxMapper.update(xxx, updateWrapper);

相关文章

暂无评论

暂无评论...