解决MyBatis-Plus更新对象⽆法设空值(含代码具体使⽤)
异常信息
原因
因为 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=?
//执⾏结果的异常信息
batis.anslateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
batis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy105.update(Unknown Source)
batis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
at ute(MybatisMapperMethod.java:69)
at verride.MybatisMapperProxy.invoke(MybatisMapperProxy.java:61)
at com.sun.proxy.$Proxy159.updateById(Unknown Source)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.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::Id()).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(Tim
estamp), admin(String), 2020-08-11 16:25:50.0(Timestamp), null, 791(Long) Updates: 1
附带mybatis-plus 表字段标识注解类以及相应字段策略枚举类
/*
* Copyright (c) 2011-2020, hubin (jobob@qq).
* <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>
* /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 batisplus.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.pehandler.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).
* <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>
* /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 batisplus.annotation;
/**
* 字段策略枚举类
*
* @author hubin
* @since 2016-09-09
*/
public enum FieldStrategy {
/**
* 忽略判断
*/
IGNORED,
/**
* ⾮NULL判断
*/
NOT_NULL,
/**
unknown怎么处理* ⾮空判断
*/
NOT_EMPTY,
/**
* 默认的,⼀般只⽤于注解⾥
* <p>1. 在全局⾥代表 NOT_NULL</p>
* <p>2. 在注解⾥代表跟随全局</p>
*/
DEFAULT
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论