Mybatis-plus 更新字段的时候设置为null,忽略实体null判断之后,报`Cause: org.apache.ibatis.type.TypeException:Error sett...

Mybatis-plus 更新字段的时候设置为null,忽略实体null判断之后,报Cause: org.apache.ibatis.type.TypeException:Error setting null for parameter #1 with JdbcType OTHER错误,解决

问题1

在用mybatis-plus封装的updateById方法来更新数据时,想把一个字段设置为null值,但是发现更新后数据没有为null还是原来的值,这是因为mybatis-plus在更新的时候做了null判断,默认不更新为null的传参。

解决

在实体类对应的字段上加注解@TableField(strategy=FieldStrategy.IGNORED),忽略null值的判断,例如

方式1

@TableField(strategy = FieldStrategy.IGNORED)
private String name;
当@TableField注解有多个值时
@TableField(value = "user_name", strategy = FieldStrategy.IGNORED)
private String userName;
package com.baomidou.mybatisplus.annotation;

/**
 * <p>
 * 字段策略枚举类
 * </p>
 *
 * @author hubin
 * @since 2016-09-09
 */
public enum FieldStrategy {
    /**
     * 忽略判断
     */
    IGNORED,
    /**
     * 非NULL判断
     */
    NOT_NULL,
    /**
     * 非空判断
     */
    NOT_EMPTY,
    /**
     * 默认的,一般只用于注解里
     * 1. 在全局里代表 NOT_NULL
     * 2. 在注解里代表 跟随全局
     */
    DEFAULT
}

方式2

/** * 姓名 */

@TableField(fill = FieldFill.UPDATE)

private String name;

问题2

按上述方式修改后,更新时报Cause: org.apache.ibatis.type.TypeException:Error setting null for parameter #1 with JdbcType OTHER的异常

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='et.contractEndTime', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #14 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
### The error may involve com.deepexi.middle.customer.mapper.CustomerAuthorizeMapper.updateById-Inline
### The error occurred while setting parameters
### SQL: UPDATE customer_authorize  SET contract_start_time=?, contract_end_time=?, tenant_id=?, app_id=?,  updated_time=?  WHERE id=?   AND is_deleted=0
### Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='et.contractEndTime', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #14 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111

解决

添加配置

配置 jdbcTypeForNull=NULL Spring Bean 配置方式:

MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);//开启下划线转驼峰
sqlSessionFactory.setConfiguration(configuration);

yml 配置

mybatis-plus:
  configuration:
    jdbc-type-for-null: 'null' 

properties配置

mybatis-plus.configuration.jdbc-type-for-null=null
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。