简体   繁体   English

检索键值对作为映射

[英]Retrieve Key-Value Pairs as Map

I'm trying to retrieve two columns from the DB with iBatis. 我正在尝试使用iBatis从数据库中检索两列。 I want to do it as a Map, such that resultMap.get(col1) == col2Obj . 我想将其作为Map进行,这样resultMap.get(col1) == col2Obj

The same question was asked here , but the posted solution won't work for me, and fails with the error: Cannot cast List as Map . 这里提出同样的问题,但是发布的解决方案对我不起作用,并因以下错误而失败: Cannot cast List as Map I am using iBatis 2.3.4. 我正在使用iBatis 2.3.4。 (Is this feature available in this version? I can't seem to find the relevant documentation.) (此功能在此版本中可用吗?我似乎找不到相关的文档。)

My statement map: 我的陈述图:

<resultMap id="countMap" class="java.util.HashMap">
    <result property="key" column="KEY" />
    <result property="value" column="VALUE" />
</resultMap>
<select id="getCounts" resultMap="countMap" parameterClass="map">
SELECT
    TYPE AS KEY,
    SUM(VAL) AS VALUE
FROM MYTABLE
WHERE REGDATE BETWEEN #start_date# and #end_date#
GROUP BY TYPE
</select>

The actual query returns: 实际查询返回:

|| KEY || VALUE ||
|| 4   || 304   ||

Calling .toString() on the Map that returns: 在返回的地图上调用.toString()

{"key" : "4", "value" : "304" }

Since the keyset consists of {"key", "value"}, the resultMap doesn't appear to be doing what I want it to do. 由于键集由{“ key”,“ value”}组成,因此resultMap似乎没有按照我想要的去做。 When I call the query with parameters that would result in more than 1 row, I get the error. 当我使用将导致超过1行的参数调用查询时,出现错误。

I am using sqlMapClient.queryForMap("mymap.getCounts", args); 我正在使用sqlMapClient.queryForMap("mymap.getCounts", args); to execute the query. 执行查询。

Wups, I got it. 哇,我明白了。

I needed to use 我需要用

sqlMapClient.queryForMap("mymap.getCounts", args, "key", "value");

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

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