Search code examples
javasortinghashmapibatis

How to get a sorted result in iBatis?


I have a table mgr_employee with 2 columns managerName, teamEmployee.
Although I do a sort in sql I get unsorted resultMap in java.
How can I get a sorted map? Why does iBatis mix up the resultMap?

<resultMap id="s_filter_defaults_ResultMap" class="java.util.HashMap">
  <result property="key" column="managerName"/>
  <result property="value" column="count"/>
</resultMap>

<select id="mCount" parameterClass="java.util.HashMap" resultMap="mcount_ResultMap">
    <![CDATA[
     select managerName, count(teamEmployee) AS count
      from mgr_employee
      group by managerName 
      order by managerName;
    ]]>
</select>

Java code to call the above sql:

Map<String,Long> mCountMap = getSqlMapClientTemplate().queryForMap("mCount", "", "key", "value");

mCountMap is not sorted as was expected due to the "order by" clause in the sql. Any comments/suggestion, how to get the resultMap sorted?


Solution

  • I think your problem might be with the Java types used. To need to query for a list, not for a Map, because the Java HashMap (I suppose this is what the query will return) doesn't have support for sorting. See the SqlMapDaoTemplate#queryForList() methods that should return what you need.