列号作为mybatis select中的hashmap键

时间:2012-12-20 13:40:40

标签: mybatis

我在mybatis中有动态选择查询,如下所示:

<select id="genericSelect" parameterType="GenericQueryParameter" resultType="hashmap">
  SELECT <if test="selectMode != null">${selectMode}</if> ${selectPart}
  FROM ${fromPart}
  <where>
    <if test="fixedWherePart != null">${fixedWherePart}</if>
    <if test="wherePart != null">AND ${wherePart}</if>
    <if test="initialCondition != null and wherePart == null">AND ${initialCondition}</if>
  </where>
  <if test="groupByPart != null"> GROUP BY ${groupByPart}</if>
  <if test="havingPart != null"> HAVING ${havingPart}</if>
  <if test="order != null"> ORDER BY ${order}</if>
</select>

对我来说,在结果中使用hashmap键是很重要的,要填充列号,而不是列名。以某种方式可以制作一些自定义处理程序或其他任何东西吗?

Mybatis版本是3.1.1,我正在使用mapper接口来处理查询。

1 个答案:

答案 0 :(得分:0)

这在我想要的方式上是不可能的,所以我稍微改变了一下查询。首先,我将selectPart更改为数组。现在我将查询的选择部分更改为:

<foreach collection="selectArray" item="selectElement" index="index" separator=", ">
  ${selectElement} COL${index+1}
</foreach>

现在我只是使用别名和index var将元素放在hashmap中,并在查询的select部分保留顺序信息。