我使用的是 mybatis-3.2.8.jar 和 JDK 1.6.0_45。
源表名为emp
:
> EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO SHORTCUT
>
> 7499 ALLEN SALESMAN 7698 1981/02/20 00:00:00 1600 300 30 null
SQL 是这样的:
insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, SHORTCUT)
values
(#{EMPNO},#{ENAME},#{JOB},#{MGR},#{HIREDATE},#{SAL},#{COMM},#{DEPTNO},#{SHORTCUT});
我正在逐行使用此 SQL。选择 1 行然后插入,如此反复。或者认为该表只有 1 行。
然后我面临这个错误:
org.apache.ibatis.exceptions.PersistenceException:
#### Error updating database. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #7 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=insert into TB_ODS_EMP2(EMPNO,ENAME,JOB,MGR;BEGIN-OF-STATEMENT;<grant>, DRIVER=4.12.55
#### The error may involve pdss5.hs.hdw.ETLTargetMapper.insertTargetTable-Inline
#### The error occurred while setting parameters
#### SQL: insert into TB_ODS_EMP2(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO,SHORTCUT) values(?,?,?,?,?,?,?,?,?) Call getNextException to see the cause
#### Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #7 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=insert into TB_ODS_EMP2(EMPNO,ENAME,JOB,MGR;BEGIN-OF-STATEMENT;<grant>, DRIVER=4.12.55
我的领导不想像这样使用jdbyTYPE
:
insert into emp
(#{EMPNO},#{ENAME},#{JOB},#{MGR},#{HIREDATE},#{SAL},#{COMM},#{DEPTNO},`#{SHORTCUT,jdbcTYPE=VARCHAR}`)
......
MyBatis中设置空参数时不添加jdbcType有什么解决办法吗?
我认为添加 jdbcTYPE=VARCHAR
就足够了,但我的主管讨厌它(也许......)。
最佳答案
某些数据库不允许在查询中发送非类型化 null
,因此您应该发送它,否则从一个数据库供应商切换到另一个数据库供应商可能会导致您的 SQL 失败。
请参阅此处了解更多说明:Is jdbcType necessary in a MyBatis mapper?
jdbcType
实际上并不是 MyBatis 的要求,而是 JDBC 的要求。对于 null
参数,最好始终发送 jdbcType
。
关于java - MyBatis中设置空参数时不添加jdbcType有什么解决办法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30568376/