Java iBatis/MyBatis - 获取结果映射而不是带有自定义键和值的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21132589/
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
iBatis/MyBatis - Get a Map of result instead of a List with custom keys and values
提问by Sid
Here's my code:
这是我的代码:
<resultMap class="<custom_class>" id="maptoCustomClass">
<result property="idModifiedBy" column="col2" />
<result property="assignedTeamName" column="col3" />
</resultMap>
<resultMap class="java.util.HashMap" id="resultMapId">
<result property="key" column="col1"/>
<result property="value" resultMap="file.maptoCustomClass"/>
</resultMap>
<select id="fetchTaskTeamAndUser" parameterClass="java.util.HashMap" resultMap="resultMapId">
SELECT col1, col2, col3
FROM schema_name.table_name
</select>
This is not working and throwing 'Too many rows returned' error. I understand why.
这不起作用并抛出“返回的行数太多”错误。我明白为什么。
My question is how can I fetch the results as a KEY and VALUE pair of a HashMap?
我的问题是如何将结果作为 HashMap 的 KEY 和 VALUE 对获取?
e.g. I should get one HashMap with key as the value of col1 and values as the object that contains the values of col2 and col3.
例如,我应该得到一个 HashMap,key 作为 col1 的值,values 作为包含 col2 和 col3 值的对象。
采纳答案by Zaw Than oo
Try as :
尝试如下:
Map<String,Long> mCountMap = getSqlMapClientTemplate().queryForMap("mydata", "", "key", "value");
<resultMap id="hashMapResult" class="java.util.HashMap">
<result property="key" column="col_1"/>
<result property="value" column="col_2"/>
</resultMap>
<select id="mydata" resultMap="hashMapResult">
select col_1, col_2 from sometable
</select>