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

来源:互联网 发布:从零开始学编程 编辑:程序博客网 时间:2024/05/16 15:12

       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   result= sqlMapClient.queryForList("selectId");

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语句中的别名。

 

原创粉丝点击