mybatis返回带有重复条目的结果映射

时间:2012-06-12 07:09:42

标签: mybatis

我正在使用mybatis从数据库中检索数据,返回的数据包含重复的条目。

必填结果:列名,值

预期结果是:column1值A 但返回的结果是:COLUMN1值A,column1值A。

希望能够澄清我的怀疑。

有人可以告诉我它为什么会这样吗?

<select id="getContentMap" resultType="map" parameterType="map">
            select planId,location_qualifier  from disclaimer_disclosure_content where 
            <choose>
                <when test="plan_id != null">
                    plan_id = #{plan_id}
                </when>
                <when test="product_id != null">
                    product_id = #{product_id}
                </when>
                <otherwise>
                    issuer_id = #{issuer_id}
                </otherwise>
            </choose>
             and effective_date >= #{effective_date} 
             and location_qualifier LIKE  CONCAT('%' , #{location_qualifier} , '%') 
        </select>

2 个答案:

答案 0 :(得分:5)

您看到的问题是MyBatis 3中的错误,直到版本3.0.6:http://code.google.com/p/mybatis/issues/detail?id=303

在那个版本之后,你会得到我在其他答案中概述的答案(使用MyBatis 3.1.1完成)。

您有四种选择:

  1. 只需忽略它,只抓住大写或小写条目
  2. 升级至至少3.0.6
  3. 停止使用map作为resultType并移至POJO域对象
  4. 使用以下解决方法:
  5. MyBatis的解决方法&lt; 3.0.6

    使用完整的大写列别名,它们只会在地图中显示一次(大写):

    <select id="getContentMap" resultType="map" parameterType="map">
      select plan_id as PLAN_ID, location_qualifier as LOCATION_QUALIFIER from disclaimer_disclosure_content
      where
      <!-- SNIP: is the same as you had -->
    </select>
    

    这导致输出:

    {PLAN_ID=2, LOCATION_QUALIFIER=Bar}
    

    (或类似的东西取决于您的选择的确切方式)。

答案 1 :(得分:0)

您可能需要报告更多信息,例如:

  1. 您使用的是什么数据库?
  2. 你使用的是什么版本的MyBatis 3(或者你还在使用iBATIS)?
  3. 你的桌面结构是什么样的?
  4. 无论如何,我使用MySQL 5.1和MyBatis-3.1.1尝试了一个稍微简化的查询版本,它工作正常 - 这意味着我只返回结果图中列名的一个条目。我在下面提供了我的设置,因此您可以尝试重现它或诊断您的代码可能出错的地方。

    首先,您的select语句中有错误。你有

    SELECT planId
    

    然后你有:

    WHERE ... plan_id = #{plan_id}
    

    所以你可能在SELECT子句中意味着SELECT plan_id

    这对我有用。

    我稍微简化的MyBatis选择映射是:

    <select id="getContentMap" resultType="map" parameterType="map">
      SELECT plan_id, location_qualifier FROM disclaimer_disclosure_content
      WHERE
      <choose>
        <when test="plan_id != null">
          plan_id = #{plan_id}
        </when>
        <otherwise>
         product_id = #{product_id}
        </otherwise>
      </choose>
      AND location_qualifier LIKE CONCAT('%' , #{location_qualifier} , '%')
    </select>
    

    其次,我的MySQL查询表:

    mysql> select * from disclaimer_disclosure_content;
    +---------+--------------------+------------+
    | plan_id | location_qualifier | product_id |
    +---------+--------------------+------------+
    |       1 | Foo                |        101 |
    |       2 | Bar                |        102 |
    |       3 | Baz                |        103 |
    |       4 | Quux               |        104 |
    +---------+--------------------+------------+
    4 rows in set (0.01 sec)
    

    第三,我的Java代码使用映射:

    @Test
    public void testForSO() throws Exception {
      Map<String, Object> paramMap = new HashMap<String, Object>();
      paramMap.put("plan_id", 2);
      paramMap.put("location_qualifier", "Ba");
    
      List<Map<String,Object>> lmap = session.selectList("getContentMap", paramMap);
      assertNotNull(lmap);
      Map<String,Object> m = lmap.get(0);
      assertNotNull(m);
      System.out.println(m.toString());
    }
    

    这传递并打印出来:

    {location_qualifier=Bar, plan_id=2}
    

    我也尝试了

    Map<String,Object> m = session.selectOne("getContentMap", paramMap);
    

    并获得相同的预期结果。

相关问题