Mybatis-Plus在Oracle中LambdaUpdateWrapper无法更新字段为null值,报错无效的列类型:1111

现象

使用mybatis-plus 3.5.9版本中LambdaUpdateWrapper set(R column, Object value),当value为null时无法更新

报错如下

Caused by: org.apache.ibatis.type.TypeException: 
Could not set parameters for mapping:
ParameterMapping{property='ew.paramNameValuePairs.MPGENVAL4', 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 #4 with JdbcType OTHER . 
Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property.
Cause: java.sql.SQLException: 无效的列类型: 1111

上述是Oracle报的错,Mysql下却能正常更新
在这里插入图片描述
在这里插入图片描述

问题分析

org.apache.ibatis.type.BaseTypeHandler类中方法

 @Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    if (parameter == null) {
      if (jdbcType == null) {
        throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
      }
      try {
        ps.setNull(i, jdbcType.TYPE_CODE);
      } catch (SQLException e) {
        throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
              + "Cause: " + e, e);
      }
    } else {
      try {
        setNonNullParameter(ps, i, parameter, jdbcType);
      } catch (Exception e) {
        throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different configuration property. "
              + "Cause: " + e, e);
      }
    }
  }

ps.setNull(i, jdbcType.TYPE_CODE);jdbcTypeOther

public enum JdbcType {
......
  OTHER(Types.OTHER),
......

}

在这里插入图片描述
报错信息即为无效类型:1111

解决方案

  1. 使用MySQL数据库
    这种有点不太现实,但自己研究还是可以的
    目前支持Other的数据库有PostgreSQLMySQL,SQLServer
  2. 目前Mybatis-Plus3.x版本以上支持LambdUpdate方式更新null字段
  3. 配置更新策略
    全局配置:
mybatis-plus:
	global-config:
		field-strategy: IGNORE 

使用注解

    FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;

    /**
     * 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略
     * <p>
     * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去
     * NOT_NULL: update table_a set <if test="columnProperty != null">column=#{columnProperty}</if>
     * NOT_EMPTY: update table_a set <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
     * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
     *
     * @since 3.1.2
     */
    FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;

若只针对指定字段需要指定的jdbcType
4. 若字段为null直接赋值空串

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游侠小马哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值