当前位置: 代码迷 >> 综合 >> mybatis plus updateById方法执行出错 表单提交 解决办法
  详细解决方案

mybatis plus updateById方法执行出错 表单提交 解决办法

热度:18   发布时间:2024-02-28 08:47:46.0

1.问题描述:

mybatis plus updateById方法执行出错

表单提交时,某些字段为null,mybatisplus对这些字段更新不成功

 

2.出现异常:

ReflectionException: There is no getter for property named 'null'

 

3.问题分析:

由于Mybatis plus默认的更新策略FieldStrategy 是NOT_NULL:非 NULL;即通过接口更新数据时数据为NULL值时将不更新进数据库。

FieldStrategy 有三种策略:IGNORED:0 忽略
NOT_NULL:1 非 NULL,默认策略
NOT_EMPTY:2 非空

所以,如果实体类A属性B为NULL,那么updateById(entity)方法时,属性B会被忽略掉,那么mybatisplus的update语句就get不到属性B,即B属性对应的位置就为null,所以There is no getter for property named 'null' in class A

 

4.解决办法:

Mybatis plus通过updateById(XXX)更新数据,当用户有更新字段为 空字符串 或者 null 的需求时,

 

首先在实体类注明主键

    @TableId(value = "worksheet_id")private String worksheetId;

接着,

方法1:对 FieldStrategy 策略进行调整

#mybatis
mybatis-plus:global-config:field-strategy: 0

方法2:修改属性的更新策略,即更新时不做判断,不忽略null值

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

 

  相关解决方案