问题描述:
在使用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” “
<configuration>
…
<settings>
<setting name="jdbctypefornull" value="null" />
</settings>
…
</configuration>
这样,即使传入参数为null,mybatis也能够将其转换成正确的数据类型,并进行存储操作。
如对本文有疑问, 点击进行留言回复!!
Oracle 基本概念 Dadabase,schema,user,table...
dbeaver连接Oracle中文乱码的解决方案--druid
Oracle常用语句(时间格式、去重、去特殊符号、查询表空间等)
Navicate 如何导出数据库中的存储过程、事件、视图等?
每日一记:Oracle升级2020年4月份的数据库补丁Database Patch Set Update : 11.2.0.4.200414 (30670774)
网友评论