简体   繁体   中英

Select query in MyBatis using resultMap fetches only one row

We are using MyBatis for one of the projects I am working on. I am facing a problem while trying to fetch results using a resultMap.

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

The important things to note in the above file is that we are fetching the column named CTN_DTR_NM twice in the select query through a different alias (See Value_Columns_List) . This is because the channel_map table contains a CTN_DTR_NM. The channel map table also contains a reference to another table which contains a CTN_DTR_NM.

The problem I am facing is that even though the channel_map table contains multiple rows, the select method returns a List that contains only one row. Checking the MyBatis logs shows that the actual query fetched multiple rows and all the fetched rows are displayed in the logs. I have a feeling the problem has got something to do with the way the result set fetched by MyBatis is being mapped against my POJOs.

as far as you use the id tag for the chnlMapId property, you are preventing mybatis to generate more than one object with the same value of chnlMapId. ¿are you trying to use result tag instead of id tag for the chnlMapId property?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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