您的当前位置:首页正文

mybatis查询子表

2024-11-08 来源:个人技术集锦

今天来记录一下 mybatis自动查询子表数据

查询结果类如下:

public class SoundPageVO {

    private String indexId;

    private String providerId;

    private String providerName;

    private Integer soundType;

    private List<SoundInfoVO> soundInfoVOList;

}

方法一:

xml文件如下:

    <resultMap type = "com.xxx.robot.vo.SoundPageVO" id = "SoundPageMap" autoMapping = "true">
        <id column="index_id" property = "indexId"/>
        <association property = "soundInfoVOList" javaType = "java.util.List" autoMapping = "true" select = "querySoundInfoById" column = "index_id(主表的id字段)"/>
    </resultMap>

    <select id = "querySoundInfoById" resultType = "com.xxx.robot.vo.SoundInfoVO">
        <!--这里是子查询实体-->
        select * from where id = #{id}
    </select>

方法二:

<resultMap type = "com.cashway.robot.vo.SoundPageVO" id = "SoundPageMap" >
        <id column="index_id" property = "indexId"/>
        <id column="provider_id" property = "providerId"/>
        <id column="provider_name" property = "providerName"/>
        <id column="sound_type" property = "soundType"/>
        <collection  property = "soundInfoVOList" ofType = "com.cashway.robot.vo.SoundInfoVO" >
            <result  column="sound_code" property="soundCode"/>
            <result  column="sound_code_parent" property="soundCodeParent"/>
            <result  column="code_name" property="codeName"/>
        </collection >
    </resultMap>

显示全文