异常信息
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘null’ in ‘class com.XXX.XXX.entity.XXX’
原因
因为 MyBatis-Plus 自带的更新方法,都有对对象空值进行判空。只有不为空的字段才会进行数据更新。想要给对象中的某个值,设置为null值,也是如此。
解决方式
在实体类对应的字段上加注解@TableField(strategy=FieldStrategy.IGNORED),忽略null值的判断,例如:
@TableField(value = "remark" ,strategy = FieldStrategy.IGNORED)
private String remark;
并且使用自带的update方法,不要使用自带的updateById方法.
示例
1、使用updateById方法:
@TableField(value = "remark" ,strategy = FieldStrategy.IGNORED)
private String remark;
后端代码:
xxxMapper.updateById(entity);
//执行sql:
关键点就在这里where条件后面自动拼接的sql是 null = ? 而不是我们所想的id=? .
UPDATE XXX SET status=?, del_flag=?, create_time=?, update_by=?, update_time=? WHERE null=?
//执行结果的异常信息
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'null' in 'class com.XXX.XXX.entity.XXX'
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisE