4

I can't seem to find a way to get the result map as a map

My sql is

<select id="retrievePackageHeader" parameterType="java.lang.String" resultType="PackageHeaderMap">
    SELECT CONCAT(SCE_WRK_CTRL_NB, AC_CRR_CDE) as row_id, 
    MTC_CHK_TYP_CDE, 
    PLNR_REVW_IND, 
    PLNR_OWD_IND, 
    PKG_SLOT_TYP_CDE 
    FROM WSM_PKG_HDR WHERE AC_NB = '${value}';
    WITH UR
</select>

Now i need row_id as the map (key) and the other columns as attributes of a bean.

I want to do something like my code below, but I can't find the correct syntax.

 <resultMap id="PackageBeanResult"              type="PackageBean">
    <result property="checkType"                column="MTC_CHK_TYP_CDE"/>
    <result property="plannerReview"            column="PLNR_REVW_IND"/>
    <result property="plannerOwned"             column="PLNR_OWD_IND" />
    <result property="slotType"                 column="PKG_SLOT_TYP_CDE" />
 </resultMap>

 <resultMap id="PackageHeaderMap"               type="java.util.HashMap">
    <result property="java.lang.String"         column="row_id"/>
    <result property="object"                   resultMap="PackageBeanResult"/>
 </resultMap>

Any ideas?

Thanks.

Jordin Vell
  • 141
  • 1
  • 2
  • 11

3 Answers3

3

In my case, add helper class:

/**
 * Helper converting list to map.
 * @param <K> key
 * @param <V> value
 */
@Getter
public class MappingHelper<K, V> {
    private K key;
    private V value;

    /**
     * Return map from {@link MappingHelper} list.
     * @param list DTO list
     * @param <K> key
     * @param <V> value
     * @return map
     */
    public static <K, V> Map<K, V> toMap(List<MappingHelper<K, V>> list) {
        if (list == null) {
            return Collections.emptyMap();
        }
        return list.parallelStream().collect(Collectors.toMap(MappingHelper::getKey, MappingHelper::getValue));
    }
}

And, init mapper.java:

List<MappingHelper<Integer, String>> getNames(@Param("ids") List<Integer> Ids);

mapper.xml:

<resultMap id="nameMap" type="package.model.MappingHelper">
    <id     property="key"   column="id"/>
    <result property="value" column="nm"/>
</resultMap>

<select id="getNames" resultMap="nameMap">
    SELECT id, nm
    FROM name_table
    WHERE id IN <foreach item="id" collection="ids" open="(" separator="," close=")">#{id}</foreach>
</select>

Finally, use as follows:

Map<Integer, String> names = MappingHelper.toMap(mapper.getNames(ids));
Yeongjun Kim
  • 739
  • 7
  • 19
1

Mybatis does not support the feature you wanted. Why do you just use java.util.Map as your result set directly.

<select id="retrievePackageHeader" parameterType="java.lang.String" resultType="java.util.Map">
SELECT CONCAT(SCE_WRK_CTRL_NB, AC_CRR_CDE) as row_id, 
MTC_CHK_TYP_CDE as checkType, 
PLNR_REVW_IND as plannerReview, 
PLNR_OWD_IND as plannerOwned, 
PKG_SLOT_TYP_CDE as slotType
FROM WSM_PKG_HDR WHERE AC_NB = '${value}';
WITH UR

Blank
  • 12,308
  • 1
  • 14
  • 32
1

Its very simple.

If you use Interface as Dao, you have to add this annotation:

@MapKey("key")
public Map<String,Object> searchSomethings(... parameters ...);

You will have, in your query, a column with an alias as "key":

SELECT
  column_key as key,
  foo_column as fooColumn,
  ...
FROM table

In select statment, you have to leave resultType with Object class.

<select id="searchSomethings" resyltType="ObjectClass">
  ...
</select>
RetroMime
  • 387
  • 1
  • 8
  • 23
  • Alas, MySQL has `key` as a reserved word. https://stackoverflow.com/questions/924265/what-does-the-key-keyword-mean – Chloe Dec 07 '18 at 19:06
  • After quoting the keyword (`@Select("select sources.host as 'key', count(*) total ..."`), it now gives `nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'key' in 'class java.lang.String'`. – Chloe Dec 07 '18 at 19:34
  • Mybatis doesnt care about reserved words when you are usin them in a select statement – Germán Sánchez Pastor Mar 09 '23 at 06:33