ibatis返回List

需求:返回電話號碼列表,之前查詢多個字段時用Map作爲返回類型,現在單個字段列表用List。

最開始的寫法:

<select id="selectCellListByShopId" resultClass="List" parameterClass="java.lang.Long">
select P.TEL_PHONE
  from SHOP_PHONE P
  WHERE P.SHOP_ID =#shopId:BIGINT# 
  and P.IS_DEL = '0' and P.IS_ACTIVE = '1'
</select> 

這樣查到的結果是[]列表,後來在老外的一篇文章上找到的解決方案,地址如下:

http://ibatis.10938.n7.nabble.com/return-simple-String-or-List-lt-String-gt-td17133.html

其中重點如下:

Tomas, 

I am assuming that you have not read the developers guide and I just 
giving things a go.  You need to read the guide completely. 

In this case Guy was saying that if your query only returns a string 
then set the resultType to String.  Then use the ibatis queryForList() 
to compile your results into a List<String>. 


Nathan 

按照這個方案修改後的代碼如下:

<select id="selectCellListByShopId" resultClass="java.lang.String" parameterClass="java.lang.Long">
select P.TEL_PHONE
  from SHOP_PHONE P
  WHERE P.SHOP_ID =#shopId:BIGINT# 
  and P.IS_DEL = '0' and P.IS_ACTIVE = '1'
</select>


Dao層通過queryForList()方法查詢出來即可。


小結:遇到這種問題的常用解決方案

        1.查詢ibatis的官方文檔.

        2.Google 關鍵字ibatis resultClass.




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