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。



轉自http://www.myexception.cn/program/675170.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章