ibatis传入list对象

在使用ibatis的时候经常需要传入list对象,sql语句如下。

<select id="GET-PERSONS" parameterClass="java.util.ArrayList" resultClass="pojo.Person">
    <![CDATA[
            select * from  person where id in
    ]]>
    <iterate  open="(" close=")" conjunction=",">  
            #list[]#
    </iterate>
</select>

这个是简单的sql语句,对于list中是别的对象的,比如List<Person>这个参数传进来时需要这样使用

<select id="GET-PERSONS" parameterClass="java.util.List" resultClass="pojo.Person">
    <![CDATA[
            select * from  person where id in
    ]]>
    <iterate open="(" close=")" conjunction=",">  
        <![CDATA[ 
            #list[].id#
        ]]>
    </iterate>
</select>

注意:上面select语句入参用的是parameterClass是java.util.ArrayList类,而不是一个map,这时iterator语句就不需要有property="ids"属性,即

<select id="GET-PERSONSS" parameterClass="java.util.ArrayList" resultClass="pojo.Person">
    <![CDATA[
            select * from  person where id in
    ]]>
    <iterate property="ids" open="(" close=")" conjunction=",">  
            #ids[]#
    </iterate>
</select>

如果有这个属性的话ibatis会从参数中去找属性为ids这个对象,而参数是一个list没有这个属性,因此就会报错:

Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in person-sqlmap.xml.
--- The error occurred while preparing the mapped statement for execution.
--- Check the person.GET-PERSONS.
--- Check the parameter map.
--- Cause: com.ibatis.common.beans.ProbeException: Error getting ordinal list fr
om JavaBean. Cause java.lang.StringIndexOutOfBoundsException: String index out o
f range: -1

如果parameter使用map,那么需要在java代码中将这个list封装进map。

原文地址:https://www.cnblogs.com/longshiyVip/p/4753452.html