oneselfone

本文详细介绍了使用MyBatis进行多表关联查询的方法,包括one2many和many2one类型的关联查询,通过具体的XML配置示例,展示了如何在不同场景下实现多表查询,适用于希望提高数据库操作效率的开发者。

1.one2many-多表连接查询


<mapper namespace="com.abc.dao.INewsLabelDao"> <!-- 多表连接查询 --> <resultMap type="NewsLabel" id="newslabelMap"> <id column="ptid" property="id"/> <result column="ptname" property="name"/> <collection property="children" ofType="NewsLabel"> <id column="cdid" property="id"/> <result column="cdname" property="name"/> </collection> </resultMap> <select id="selectNewsLabelById" resultMap="newslabelMap"> select pt.id ptid,pt.name ptname,cd.id cdid,cd.name cdname from newslabel pt,newslabel cd where pt.id=cd.pid and pt.id=#{xxx} </select> </mapper>

2.one2many-多表单独查询

 

<mapper namespace="com.abc.dao.INewsLabelDao">
    
    <!-- 多表单独查询 -->

    <select id="selectChildrenByParent" resultMap="newslabelMap">
        select id,name from newslabel where pid=#{jjj}
    </select>

    <resultMap type="NewsLabel" id="newslabelMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="children" 
                    ofType="NewsLabel"
                    select="selectChildrenByParent"
                    column="id"/>
    </resultMap>

    <select id="selectNewsLabelById" resultMap="newslabelMap">
        select id,name from newslabel where id=#{xxx}
    </select>

</mapper>

3.one2many-多表单独查询2

<mapper namespace="com.abc.dao.INewsLabelDao">
    
    <!-- 多表单独查询 -->

    <select id="selectChildrenByParent" resultMap="newslabelMap">
        select id,name from newslabel where pid=#{jjj}
    </select>

    <resultMap type="NewsLabel" id="newslabelMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="children" 
                    ofType="NewsLabel"
                    select="selectChildrenByParent"
                    column="id"/>
    </resultMap>

</mapper>

4.many2one-多表单独查询

<mapper namespace="com.abc.dao.INewsLabelDao">
    
    <!-- 多表单独查询 -->

    <resultMap type="NewsLabel" id="newslabelMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association property="parent" 
                     javaType="NewsLabel"
                     select="selectNewsLabelById"
                     column="pid"/>
    </resultMap>
    
    <select id="selectNewsLabelById" resultMap="newslabelMap">
        select id,name,pid from newslabel where id=#{jjj}
    </select>

</mapper>

 

转载于:https://www.cnblogs.com/csslcww/p/9912371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值