使用MyBatis的@Param注解时参数为空值NULL的处理方法

3 篇文章 1 订阅
1 篇文章 0 订阅

最近在使用MyBatis的@Param注解映射参数到SQL时遇到下面的报错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='outflowCause', 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 #3 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111

原因是 outflowCause 这个参数为空,MyBatis默认配置的情况下会把空值的参数的 jdbcType 设置为 OTHER,这个情况下,Oracle数据库无法识别这种类型,MySql数据库可以正常解析。我这里用的是Oracle数据库,所以报了这个错误。

其中,

Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property.


尝试为此参数设置不同的 JdbcType 或不同的 jdbcTypeForNull 配置属性。

就是下面介绍的2种解决办法。


解决方法一:

在SQL中用 #{} 引用参数的时候,在其参数名称后,根据你这个参数的类型加上对应的 jdbcType 属性,如: #{outflowCause, jdbcType=VARCHAR} 

@Update("UPDATE IZ6LN_ISSUE_MODEL_NEW2 SET \"outflowCause\" = #{outflowCause,jdbcType=VARCHAR} WHERE \"id\" = #{workflowId} AND \"sequenceStatus\" = 'PROCESSING'")
int updateFAFromSRM(@Param("workflowId")String workflowId,@Param("outflowCause")String outflowCause);

解决方法二:

修改myBatis的全局配置,在 configuration 下添加 jdbc-type-for-null: 'null',我项目的 mybatis 配置写在application.yml

mybatis-plus:
  #mapper-locations: classpath:com/authie/cloudpivot/web/api/mapper/xml/*.xml
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.authie.cloudpivot.web.api.entity
  configuration:
    map-underscore-to-camel-case: true
    jdbc-type-for-null: 'null'
  # 数据库类型  支持 MySQL、ORACLE
  dbtype: oracle

解决方法二的这种修改mybatis配置文件的方法,会对所有参数为空值的情况,用NULL来填充处理。


相关文章: MyBatis JdbcType介绍 - MyBatis中文官网

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值