本文介绍了Mybatis ResultMap是HashMap< String,Object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到一种将结果图作为地图获取的方法

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

我的sql是

<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>

现在,我需要row_id作为映射(键),而其他列则作为bean的属性.

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>

有什么想法吗?

谢谢.

推荐答案

在我的情况下,添加帮助程序类:

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));
    }
}

然后,初始化mapper.java:

And, init mapper.java:

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

mapper.xml:

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>

最后,使用如下:

Map<Integer, String> names = MappingHelper.toMap(mapper.getNames(ids));

这篇关于Mybatis ResultMap是HashMap&lt; String,Object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:23