简体   繁体   English

iBatis / MyBatis-获取结果映射,而不是带有自定义键和值的列表

[英]iBatis/MyBatis - Get a Map of result instead of a List with custom keys and values

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对获取?

eg 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,其键为col1的值,值作为包含col2和col3的值的对象。

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>

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

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