mybatis 返回结果映射有重复项

JeffreyZhao 阅读:24 2024-11-01 17:39:52 评论:0

我正在使用 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> 

请您参考如下方法:

您看到的问题是 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. 使用以下解决方法:

MyBatis < 3.0.6 的解决方法

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

<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} 

(或类似的东西,具体取决于您选择的外观)。


标签:MyBatis
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号