1

我得到这个代码的例外

 List<String> addlCarrierClause=new ArrayList<String>();

<select id="GET_SEARCH_RESULTS" parameterClass="Map" resultClass="HashMap">

<isNotEmpty  prepend=" AND " property="addlClauseGtwyTemp">
    l.imp_gtwy_i in
        <iterate property="addlClauseGtwyTemp" open="(" close=")" conjunction=",">
            #addlClauseGtwyTemp[]#
        </iterate>
</isNotEmpty>

com.ibatis.common.jdbc.exception.NestedSQLException

The error occurred while preparing the mapped statement for execution.  
--- Check the GET_SEARCH_RESULTS.  
--- Check the parameter map.  
--- Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:45)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)

我使用iterate tag正确吗?
我还需要检查什么来实现这个

4

1 回答 1

3

<iterate>仅当您想循环浏览项目列表时才使用 iBatis标签。您的迭代标记用法是正确的,并且您的属性addlClauseGtwyTemp存在,否则您将得到一个NullPointerException您没有得到的。因此,您的问题只是您的addlClauseGtwyTemp不是列表或可迭代集合(实现可迭代接口),而是一个平面值。

我能够在本地模拟您的问题,并且在我的日志中得到了相同的堆栈跟踪

2013-09-02 17:04:59,724 ERROR UNEXPECTED_EXCEPTION: An unexpected error occurred processing wlm.search
com.xyz.event.InvalidEventStateException: wlm.searchCO:
--- The error occurred in workListManagementSearchService.xml.
--- The error occurred while preparing the mapped statement for execution.
--- Check the getWLMSearchCustomers.
--- Check the parameter map.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.
        at com.xyz.event.Query.execute(Query.java:67)
        at com.xyz.event.AbstractParentExecutable.executeChildren(AbstractParentExecutable.java:35)
        at com.xyz.event.Event.execute(Event.java:32)
        at com.xyz.event.impl.d$a.processEvent(EventContextFactory.java:56)

解决方案

确保您的属性addlClauseGtwyTemp是列表或集合,而不是平面值。

列表

addlClauseGtwyTemp = [Asia,America,Austalia,Africa,SouthAmerica,Europe] 

非清单

addlClauseGtwyTemp = Asia

额外提示

在您的情况下,您正在使用标签保护和检查此属性addlClauseGtwyTemp不为空且不为空。("" or size() < 1)<isNotEmpty>

好吧,这很好,我还要赞扬 using<isNotEmpty>比 using 更有效,<isNotNull>因为<isNotNull>只检查属性是否不为空,但不检查属性addlClauseGtwyTemp是否不为空,即属性addlClauseGtwyTemp的大小小于 1 size() < 1. 如果它为 null 或大小小于 1,则嵌套在该标记中的代码将不会运行。但是,如果该属性不符合您的<isNotEmpty>条件,会发生什么?使用您在问题中粘贴的代码,查询将引发错误。这意味着您必须有一个前面的代码来执行与此相反的操作,以处理条件无法满足的情况。

<select id="GET_SEARCH_RESULTS" parameterClass="Map" resultClass="HashMap">

    <isNotEmpty  prepend=" AND " property="addlClauseGtwyTemp">
        l.imp_gtwy_i in
        <iterate property="addlClauseGtwyTemp" open="(" close=")" conjunction=",">
        #addlClauseGtwyTemp[]#
        </iterate>
    </isNotEmpty>

    <isEmpty  prepend=" AND " property="addlClauseGtwyTemp">
       1=2
    </isEmpty>

    <isNull property="addlClauseGtwyTemp">
          select 1 from dual (Oracle syntax)
          select 1 (Postgres/MySQL)
    </isNull>

</select>

我添加了一个片段来使用<isEmpty>标签检查 isNotEmpty 的反向,我使用<isNull>. 如果它为空,则查询什么也不做,只会抛出错误。

于 2013-09-02T09:41:28.800 回答