我似乎无法找到将结果地图作为地图
的方法我的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作为map(key),其他列作为bean的属性。
我想做下面的代码,但我找不到正确的语法。
<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>
有什么想法吗?
感谢。
答案 0 :(得分:2)
就我而言,添加帮助类:
/**
* 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));
}
}
并且,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>
最后,使用如下:
Map<Integer, String> names = MappingHelper.toMap(mapper.getNames(ids));
答案 1 :(得分:1)
Mybatis不支持您想要的功能。为什么直接使用java.util.Map作为结果集。
<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
答案 2 :(得分:1)
非常简单。
如果使用Interface as Dao,则必须添加此注释:
@MapKey("key")
public Map<String,Object> searchSomethings(... parameters ...);
在您的查询中,您将拥有一个别名为“key”的列:
SELECT
column_key as key,
foo_column as fooColumn,
...
FROM table
在select statment中,您必须将resultType保留为Object class。
<select id="searchSomethings" resyltType="ObjectClass">
...
</select>