简体   繁体   English

使用resultMap在MyBatis中选择查询仅获取一行

[英]Select query in MyBatis using resultMap fetches only one row

We are using MyBatis for one of the projects I am working on. 我们正在将MyBatis用于我正在从事的项目之一。 I am facing a problem while trying to fetch results using a resultMap. 尝试使用resultMap提取结果时遇到问题。

mapper.xml 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) . 在上面的文件中要注意的重要事情是,我们通过另一个别名(请参阅Value_Columns_List)在select查询中两次提取了名为CTN_DTR_NM的列。 This is because the channel_map table contains a CTN_DTR_NM. 这是因为channel_map表包含CTN_DTR_NM。 The channel map table also contains a reference to another table which contains a CTN_DTR_NM. 频道映射表还包含对另一个表的引用,该表包含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. 我面临的问题是,即使channel_map表包含多行,select方法也会返回仅包含一行的List。 Checking the MyBatis logs shows that the actual query fetched multiple rows and all the fetched rows are displayed in the logs. 检查MyBatis日志显示,实际查询提取了多行,并且所有提取的行都显示在日志中。 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. 我感觉到问题与MyBatis提取的结果集与我的POJO映射的方式有关。

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. 只要您对chnlMapId属性使用id标记,就可以防止mybatis生成多个具有相同chnlMapId值的对象。 ¿are you trying to use result tag instead of id tag for the chnlMapId property? ¿您是否要对chnlMapId属性使用结果标签而不是id标签?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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