简体   繁体   English

如何在MyBatis foreach中迭代HashMap?

[英]How to Iterate through HashMap in MyBatis foreach?

I'm trying to produce a sql which is as below in mybatis. 我正在尝试在mybatis中生成如下所示的sql。

SELECT COL_C
FROM TBLE_1
WHERE (COL_A, COL_B) in ( ('kp','kar'),('srt','sach'));

And my input parameter type is HashMap. 我的输入参数类型是HashMap。 Now How do I generate SQL from mapper xml file. 现在如何从mapper xml文件生成SQL。 The below code throws exception saying map evaluated to null. 下面的代码抛出异常,说map被评估为null。

<select id="selectCOLC" parameterType="java.util.HashMap" resultType="String">
    SELECT COL_C
    FROM TBLE_1
    WHERE (COL_A, COL_B) in 
    <foreach item="item" collection="#{map.keySet()}" open="((" separator="),(" close="))">
        #{item},#{item.get(item)}
    </foreach>
</select>

One of the other approach is to create a class with key value fields, create a list of object and then pass the parameterType as list which would look like following. 另一种方法是创建一个具有键值字段的类,创建一个对象列表,然后将parameterType作为list传递,如下所示。

<select id="selectCOLC" parameterType="list" resultType="String">
        SELECT COL_C
        FROM TBLE_1
        WHERE (COL_A, COL_B) in 
        <foreach item="item" collection="list" open="((" separator="),(" close="))">
            #{item.getKey()},#{item.getVal()}
        </foreach>
    </select>

But is there any way to my mapper work for the first approach? 但有没有办法让我的mapper工作第一种方法? other than changing the query to union 除了将查询更改为union之外

This solution doesn't work since version 3.2 - see more in Issue #208 ! 从3.2版开始,此解决方案无效 - 请参阅问题#208中的更多内容

Finally I've the solution for HashMap 最后,我得到了HashMap的解决方案

I Should use entrySet() in order to make it iteratable 我应该使用entrySet()以使其可迭代

<select id="selectCOLC" parameterType="map" resultType="kpMap">
    SELECT COL_C
    FROM TBLE_1
    WHERE (COL_A, COL_B) in 
    <foreach item="item" collection="entries.entrySet()" open="((" separator="),(" close="))">
        #{item.key},#{item.value}
    </foreach>
</select>

One more Isue I was facing parameter name was not getting injected, Hence added @Param annotation 还有一个Isue我面临的参数名称没有被注入,因此添加了@Param注释

Hence mapper interface looks like below. 因此映射器界面如下所示。

List<TblData> selectCOLC(@Param("entries")
            HashMap<String, String> entries)

this is an example in my project and it works fine 这是我的项目中的一个例子,它工作正常

<select id="getObject" parameterType="Map" resultType="hashmap">    
    select * from TABL where 
    <foreach  collection="dataMap"  index="key" item="value"  open=""  separator=" and "  close="">
        #{key}=#{value}
    </foreach>
</select>

In your first example mybatis is looking for an entry in the parameterMap with the key "map". 在你的第一个例子中,mybatis正在使用键“map”在parameterMap中查找一个条目。 I suspect that you are actually trying to iterate the key set of the parameterMap. 我怀疑你实际上是在尝试迭代parameterMap的键集。 If you nested the map within the parameter map using the key "map" it should work. 如果使用键“map”在参数映射中嵌套映射,它应该可以工作。

In the second example you should just be able to pass the HashMap.entrySet() which provides a getKey and getValue. 在第二个示例中,您应该只能传递提供getKey和getValue的HashMap.entrySet()。

As a user of mybatis 3.5, I came through this. 作为mybatis 3.5的用户,我遇到了这个问题。

Unfortunately, none of the solutions posted here worked for me but this does: 不幸的是,这里发布的解决方案都没有为我工作,但这样做:

<foreach collection="_parameter.entrySet()" index="key" item="element" separator=",">
    MY_COLUMN = #{key} AND MY_OTHER_COLUMN = #{element}
</foreach>

So, in my case collection="_parameter.entrySet()" did the trick! 所以,在我的情况下, collection="_parameter.entrySet()"就可以了!

Moreover, none specification regarding the parameterType was needed. 而且, 没有关于parameterType的规范是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM