ibatis遍历数组对象时,填写不全,容易报错。以下情况就经常遇到问题。
<isNotNull property="typeList" prepend=" AND ">
<iterate property="typeList" conjunction="OR" open="(" close=")">
type = #typeList[]#
</iterate>
</isNotNull>
如上,遍历对象时,必须把上面信息写全面,如果发生遗漏,容易报错。
情况1:
<isNotNull property="typeList" prepend=" AND ">
<iterate conjunction="OR" open="(" close=")">
type = #typeList[]#
</iterate>
</isNotNull>
如果iterate中不写明对象名称,必然要进行报错。如下:
--- Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in ibatis/sqlmap/user.xml.
--- The error occurred while preparing the mapped statement for execution.
--- Check the StockRunLog.getStockRunLogListCount.
--- Check the parameter map.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
根据错误,还是很好识别的。
情况2:
<isNotNull property="typeList" >
<iterate property="typeList" conjunction="OR" open="(" close=")">
type = #typeList[]#
</iterate>
</isNotNull>
当缺少apend时,造成上个字段和遍历字段无连接词,会报错。如下:
--- The error occurred while applying a parameter map.
--- Check the StockRunLog.getStockRunLogListCountMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( type = 10 OR type = 11 ' at line 1]; SQL was [] for task [SqlMapClient operation]
2016-09-19 21:33:46 DEBUG [DataSourceUtils.java:327] - Returning JDBC Connection to DataSource
2016-09-19 21:33:46 DEBUG [PreparedStatementPool.java:123] - {conn-10005, pstmt-20022} enter cache
2016-09-19 21:33:46 DEBUG [AbstractHandlerExceptionResolver.java:132] - Resolving exception from handler [public com.zsx.reponse com.zsx.web.controller.stock.StockRunLogController.getStockRunLogList(java.lang.Long,java.lang.Long,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.Long,java.lang.Long,java.lang.Integer,java.lang.Integer) throws java.lang.Exception]: org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the StockRunLog.getStockRunLogListMap.
--- Check the statement (query failed).
此时,必须要加上对应的apend才行。
情况3:
有时候,代码都是完全正确的,但是也会报错。如下:
Error getting ordinal list from JavaBean.
Cause java.lang.NumberFormatException: For input string: ""
对于这种情况,还没找到原因,根据报错信息,一直没找到错误哪里。代码都正确。无奈,重新编译了一次,倒是成功了。不过,以下内容,可以做个参考。
https://issues.apache.org/jira/browse/IBATIS-344
后续再看看此情况的原因好了。