您的位置:首页 > 其它

ibatis 返回list<String[]>----resultClass="hashMap" 可以实现

2012-06-19 17:33 766 查看
select a.id as id,

count(distinct b.id) as bid,

count(distinct c.id) as cid

from user a left join user_role b on a.userId = b.userId

left join role c on b.roleId = c.roleId;

要求返回结果形式为:List<String[ ]>

String [ ] 中存放的就是三个ID值.

1.到这里,通常我的想法就是,写一个javaBean,javaBean中有这三个属性,分别和这三个值相对应,然后通过resultMap来映射结果集,

如:

<typeAlias alias="UserBean" type="com.test.javaBean.UserBean"/>

<resultMap class="UserBean" id="UserBeanPojo">

<result property="id" column="id" />

<result property="bid" column="bid" />

<result property="cid" column="cid" />

</resultMap>

<select id="selectId" resultMap="UserBeanPojo">

select a.id as id,

count(distinct b.id) as bid,

count(distinct c.id) as cid

from user a left join user_role b on a.userId = b.userId

left join role c on b.roleId = c.roleId;

</select>

List<UserBean> list = sqlMapClient.queryForList("selectId");

然后再循环迭代list集合转换为String[ ]数组,然后在放入list,最后返回。

2.
实际上是陷入了惯性思维,老是想着用resultMap,其实这里直接用resultClass="hashMap" 可以实现

就不用写javaBean了。

如下:

<select id="selectId" resultClass="java.util.HashMap">

select a.id as id,

count(distinct b.id) as bid,

count(distinct c.id) as cid

from user a left join user_role b on a.userId = b.userId

left join role c on b.roleId = c.roleId;

</select>

List [b]result= sqlMapClient.queryForList("selectId");[/b]

Map hashMap = new HashMap();

for(int i=0;i<result.size();i++) {

hashMap = (HashMap)result.get(i);

String[] areaElement = new String[3];

areaElement[0] = hashMap.get("AID").toString();


areaElement[2] = hashMap.get("BID").toString();

areaElement[3] = hashMap.get("CID").toString();


statResult.add(areaElement);

}


注意:这里的hashMap.get("AID"),键名的来源就是sql语句中的别名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐