从昨天下午到现在,一直在进行ibatis环境下的单元测试,在测试中发现了一个奇怪的现象。
我们知道在sqlMap的insert语句中,一般需要一个bean类来作为parameterClass的值,一般
来说,这个bean只要一些简单的get/set方法就可以了,但是在测试中却发现这样一个问题:
如果该bean类继承java.util.ArrayList,那么这时再进行sqlMapClient的insert则会报错。
(当然了,如果不继承java.util.ArrayList类就不会出错,继承其它类也不会出错)
下面将出现错误的环境数据列出来:
1,sqlMap
<insert id="simpleObjectTest" parameterClass="com.smartdot.galaxy.portal.resources.test.IbatisTestObject"> insert into zhangbo_test (id,name) values(#id#,#name#) </insert> |
2,
IbatisTestObject?.java
public class IbatisTestObject extends ArrayList {
private String id; private String name; public IbatisTestObject() { super(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
3,junit test
... IbatisTestObject obj=new IbatisTestObject(); obj.setId("33"); obj.setName("55"); sqlMap.insert("simpleObjectTest",obj); ... |
4,数据库中字段id,name均为varchar2
5,出错信息
com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in com/smartdot/galaxy/portal/resources/test/resources_sql_map.xml. --- The error occurred while preparing the mapped statement for execution. --- Check the simpleObjectTest. --- Check the parameter map. --- Cause: java.lang.NumberFormatException: For input string: "i" Caused by: java.lang.NumberFormatException: For input string: "i" at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:90) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:442) at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:85) at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:57) at com.smartdot.galaxy.portal.resources.test.ResourcesSqlMapTest.insertIbatisSimpleObj(ResourcesSqlMapTest.java:84) at com.smartdot.galaxy.portal.resources.test.ResourcesSqlMapTest.test(ResourcesSqlMapTest.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186) Caused by: java.lang.NumberFormatException: For input string: "i"
(更多略) |
所以建议大家不要在Ibatis中的bean不要继承java.util.ArrayList,而是使用复合模式。