Mybatis:使用bean传值,当传入值为Null时,提示“无效的列类型”的解决办法

问题描述:
在使用mybatis对数据库执行更新操作时,parametertype为某个具体的bean,而bean中传入的参数为null时,抛出异常如下:
org.mybatis.spring.mybatissystemexception: nested exception is org.apache.ibatis.type.typeexception: could not set parameters for mapping: parametermapping{property=’khzx’, mode=in, javatype=class java.lang.long, jdbctype=null, numericscale=null, resultmapid=’null’, jdbctypename=’null’, expression=’null’}. cause: org.apache.ibatis.type.typeexception: error setting null for parameter #2 with jdbctype other . try setting a different jdbctype for this parameter or a different jdbctypefornull configuration property. cause: java.sql.sqlexception: 无效的列类型: 1111

mapper中代码如下所示:
<insert id=”save” parametertype=”khjlmx”>
insert into txs_lhkh_khjlmx(id,khjl,khzx,khnr,trnr)
values(fnextid(‘txs_lhkh_khjlmx’),#{khjl},#{khzx},#{khnr},#{trnr})
</insert>
解决办法一:

经过对代码分析,是由于未指定传入参数khjl的类型,当mybatis接收到null时,无法将其正确的进行解析,进而导致上述异常。
将mapper中代码修改如下:
<insert id=”save” parametertype=”khjlmx”>
insert into txs_lhkh_khjlmx(id,khjl,khzx,khnr,trnr)
values(fnextid(‘txs_lhkh_khjlmx’),#{khjl,jdbctype=numeric},#{khzx},#{khnr},#{trnr})
</insert>
解决办法二:

在配置文件mybatis-config.xml中加入如下代码:
<?xml version=”1.0” encoding=”utf-8” ?>
<!doctype configuration public “-//mybatis.org//dtd config 3.0//en” “http://mybatis.org/dtd/mybatis-3-config.dtd”>

<configuration>

<settings>
<setting name=”jdbctypefornull” value=”null” />
</settings>

</configuration>
这样,即使传入参数为null,mybatis也能够将其转换成正确的数据类型,并进行存储操作。

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐