ibatis返回List<String>

来源:互联网 发布:windows android语言 编辑:程序博客网 时间:2024/06/06 02:49

需求:返回电话号码列表,之前查询多个字段时用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.




0 0