Java 如何在 MyBatis foreach 中遍历 HashMap?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18388936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Iterate through HashMap in MyBatis foreach?
提问by Karthik Prasad
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. Now How do I generate SQL from mapper xml file. The below code throws exception saying map evaluated to null.
我的输入参数类型是HashMap。现在如何从映射器 xml 文件生成 SQL。下面的代码抛出异常,表示 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
as 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? other than changing the query to union
但是有没有办法让我的映射器为第一种方法工作?除了将查询更改为联合之外
采纳答案by Karthik Prasad
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
我面临的另一个问题是参数名称没有被注入,因此添加了@Param
注释
Hence mapper interface looks like below.
因此映射器界面如下所示。
List<TblData> selectCOLC(@Param("entries")
HashMap<String, String> entries)
回答by Brian
In your first example mybatis is looking for an entry in the parameterMap with the key "map". I suspect that you are actually trying to iterate the key set of the parameterMap. If you nested the map within the parameter map using the key "map" it should work.
在您的第一个示例中,mybatis 正在使用键“map”在 parameterMap 中查找条目。我怀疑您实际上是在尝试迭代 parameterMap 的键集。如果您使用键“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()。
回答by foghost
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>
回答by Arcones
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, nonespecification regarding the parameterTypewas needed.
此外,没有关于规范参数类型需要。