我们正在将 MyBatis 用于我正在从事的项目之一。我在尝试使用 resultMap 获取结果时遇到问题。
映射器.xml
<resultMap id="BaseResultMap" type="com.mycompany.myproduct.dto.ChannelMap">
<id column="CHNL_MAP_ID" property="chnlMapId" jdbcType="DECIMAL" />
<result column="CHNL_MAP_NM" property="chnlMapNm" jdbcType="VARCHAR" />
<association property="ctnDlvryPltf"
resultMap="com.mycompany.myproduct.mapper.ContentDelvryPltfrmMapper.BaseResultMap" />
<association property="ctnDtr"
resultMap="com.mycompany.myproduct.mapper.ContentDistributorMapper.BaseResultMap" />
<association property="region"
resultMap="com.mycompany.myproduct.mapper.RegionMapper.BaseResultMap" />
</resultMap>
<sql id="Value_Columns_List">
cmap.CHNL_MAP_NM, cdp.CTN_DLVRY_PLTF_NM,
cdp.CTN_DLVRY_PLTF_TYP_NM, cdp.CTN_DTR_NM, cd.CTN_DTR_NM,
cmap.RGN_ID, cmap.CNTRY_ID
</sql>
<select id="select" resultMap="BaseResultMap">
select
<include refid="Value_Columns_List" />
FROM
channel_map cmap,
(SELECT
cdpl.ctn_dlvry_pltf_id,cdpl.ctn_dlvry_pltf_nm,
cdplt.ctn_dlvry_pltf_typ_nm ,cds.ctn_dtr_nm FROM
content_delvry_pltfrm
cdpl,
content_delvry_pltfrm_typ cdplt,
content_distributor cds
WHERE
cdpl.ctn_dlvry_pltf_typ_id =
cdplt.ctn_dlvry_pltf_typ_id AND
cdpl.ctn_dtr_id = cds.ctn_dtr_id)
cdp,
content_distributor
cd
WHERE
cmap.ctn_dlvry_pltf_id = cdp.ctn_dlvry_pltf_id AND
cmap.ctn_dtr_id = cd.ctn_dtr_id
</select>
在上述文件中需要注意的重要一点是,我们在选择查询中通过不同的别名(参见 Value_Columns_List)两次获取名为 CTN_DTR_NM 的列。这是因为 channel_map 表包含一个 CTN_DTR_NM。频道映射表还包含对另一个包含 CTN_DTR_NM 的表的引用。
我面临的问题是,即使 channel_map 表包含多行,select 方法也会返回一个仅包含一行的 List。查看 MyBatis 日志显示实际查询获取了多行,并且所有获取的行都显示在日志中。我感觉问题与 MyBatis 获取的结果集映射到我的 POJO 的方式有关。