概述
异常信息
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(MyBatisExceptionTranslator.java:77)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy105.update(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:69)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:61)
at com.sun.proxy.$Proxy159.updateById(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy160.updateById(Unknown Source)
2、使用update方法
@TableField(value = "remark" ,strategy = FieldStrategy.IGNORED)
private String remark;
后端代码:
xxxMapper.update(entity,taskInspectionProject,new UpdateWrapper<Entity>().lambda().eq(Entity::getId,entity.getId()).set(Entity::getCreateBy,null));
//执行SQL
UPDATE XXX SET status=?, del_flag=?, create_time=?, update_by=?, update_time=?, create_by=? WHERE task_inspection_project_id = ?
Parameters:
0(String), 0(String), 2020-08-11 16:25:50.0(Timestamp), admin(String), 2020-08-11 16:25:50.0(Timestamp), null, 791(Long)
Updates: 1
附带mybatis-plus 表字段标识注解类以及相应字段策略枚举类
/*
* Copyright (c) 2011-2020, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.annotation;
import java.lang.annotation.*;
/**
* 表字段标识
*
* @author hubin sjy tantan
* @since 2016-09-09
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TableField {
/**
* 字段值(驼峰命名方式,该值可无)
*/
String value() default "";
/**
* 当该Field为类对象时, 可使用#{对象.属性}来映射到数据表.
* <p>支持:@TableField(el = "role, jdbcType=BIGINT)</p>
* <p>支持:@TableField(el = "role, typeHandler=com.baomidou.springcloud.typehandler.PhoneTypeHandler")</p>
*/
String el() default "";
/**
* 是否为数据库表字段
* <p>默认 true 存在,false 不存在</p>
*/
boolean exist() default true;
/**
* 字段 where 实体查询比较条件
* <p>默认 `=` 等值</p>
*/
String condition() default "";
/**
* 字段 update set 部分注入, 该注解优于 el 注解使用
* <p>例如:@TableField(.. , update="%s+1") 其中 %s 会填充为字段</p>
* <p>输出 SQL 为:update 表 set 字段=字段+1 where ...</p>
* <p>例如:@TableField(.. , update="now()") 使用数据库时间</p>
* <p>输出 SQL 为:update 表 set 字段=now() where ...</p>
*/
String update() default "";
/**
* 字段验证策略
* <p>默认追随全局配置</p>
*/
FieldStrategy strategy() default FieldStrategy.DEFAULT;
/**
* 字段自动填充策略
*/
FieldFill fill() default FieldFill.DEFAULT;
/**
* 是否进行 select 查询
* <p>大字段可设置为 false 不加入 select 查询范围</p>
*/
boolean select() default true;
}
字段策略枚举类
/*
* Copyright (c) 2011-2014, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.mybatisplus.annotation;
/**
* 字段策略枚举类
*
* @author hubin
* @since 2016-09-09
*/
public enum FieldStrategy {
/**
* 忽略判断
*/
IGNORED,
/**
* 非NULL判断
*/
NOT_NULL,
/**
* 非空判断
*/
NOT_EMPTY,
/**
* 默认的,一般只用于注解里
* <p>1. 在全局里代表 NOT_NULL</p>
* <p>2. 在注解里代表 跟随全局</p>
*/
DEFAULT
}
最后
以上就是奋斗可乐为你收集整理的解决 MyBatis-Plus 更新对象无法设空值(含代码具体使用)的全部内容,希望文章能够帮你解决解决 MyBatis-Plus 更新对象无法设空值(含代码具体使用)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复