問題描述:在使用mybatis對資料庫執行更新操作時,parameterType為某個具體的bean,而bean中傳入的參數為null時,拋出異常如下:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache. ...
問題描述:
在使用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也能夠將其轉換成正確的數據類型,併進行存儲操作。