mybatis 一对多 两种查询方式

本文介绍了在Java中使用MyBatis进行一对多查询的两种方式:联合查询和子查询。通过实体类`Menu`展示了如何配置映射文件,包括`resultMap`和`collection`标签的使用。两种查询方式都能获取相同的数据结果,即从`hongxin_menu`表中查询到父菜单及其对应的子菜单信息。
摘要由CSDN通过智能技术生成

注意事项:  如果是多表 例如有A表,B表  A是多端,B是一端 那么就要在B端声明A端的集合 必须

上代码:

我使用的是java语言,先看实体类 这是单表的一对多 多表跟单表逻辑是一致

public class Menu extends PublicVo {
    private Integer id;
    private String name;
    private String path;
    private String component;
    private String icon;
    private Integer leaf;
    private Integer hidden;
    private String remark;
    private Integer fatherId;
    private Date menuDate;
    private List<Menu> children;
}

mybatis 查询的一对多的两种方式

第一种:多表联合查询sql

    <!--一对多查询单表 方式一-->
    <resultMap id="menuMap" type="com.example.hongxinwulian.pojo.Menu">
        <id property="id" column="id"></id>
        <!--配置menu对象中children集合的映射-->
        <collection property="children" ofType="com.example.hongxinwulian.pojo.Menu">
            <result property="fatherId" column="fatherId"></result>
            <result property="name" column="name"></result>
            <result property="path" column="path"></result>
            <result property="component" column="component"></result>
            <result property="icon" column="icon"></result>
            <result property="leaf" column="leaf"></result>
            <result property="hidden" column="hidden"></result>
            <result property="remark" column="remark"></result>
            <result property="menuDate" column="menuDate"></result>
        </collection>
    </resultMap>

    <select id="getMenuList" resultMap="menuMap,menuTotal">
        select SQL_CALC_FOUND_ROWS * from hongxin_menu as m left join hongxin_menu as m1
        on m.id = m1.fatherId where m.fatherId = 0 limit #{currentPage},#{pageSize}
    </select>

第二种:采用子查询方式

    <!--一对多查询单表 方式二-->
    <resultMap id="menuMap1" type="com.example.hongxinwulian.pojo.Menu">
        <id property="id" column="id"></id>
        <result property="fatherId" column="fatherId"></result>
        <result property="name" column="name"></result>
        <result property="path" column="path"></result>
        <result property="component" column="component"></result>
        <result property="icon" column="icon"></result>
        <result property="leaf" column="leaf"></result>
        <result property="hidden" column="hidden"></result>
        <result property="remark" column="remark"></result>
        <result property="menuDate" column="menuDate"></result>
        <!--配置menu对象中children集合的映射
         property="children" 是一端的声明的多段的结合
         ofType="com.example.hongxinwulian.pojo.Menu" //映射到集合中的pojo属性
         column="id" 是子查询用的参数
         select="getChildMenu" 调用子查询的方法名称
         -->
        <collection property="children"
                    ofType="com.example.hongxinwulian.pojo.Menu"
                    column="id"
                    javaType="ArrayList"
                    select="getChildMenu">
        </collection>
    </resultMap>
    <resultMap id="menuMap2" type="com.example.hongxinwulian.pojo.Menu">
        <id property="id" column="id"></id>
        <result property="fatherId" column="fatherId"></result>
        <result property="name" column="name"></result>
        <result property="path" column="path"></result>
        <result property="component" column="component"></result>
        <result property="icon" column="icon"></result>
        <result property="leaf" column="leaf"></result>
        <result property="hidden" column="hidden"></result>
        <result property="remark" column="remark"></result>
        <result property="menuDate" column="menuDate"></result>
    </resultMap>

    <!--主查询也就是dao层查询的如果-->
    <select id="getMenuList" resultMap="menuMap1">
      select * from hongxin_menu where fatherId = 0
    </select>
    <!--子查询-->
    <select id="getChildMenu" parameterType="java.lang.Integer" resultMap="menuMap2">
      select * from hongxin_menu where fatherId=#{id}
    </select>

代码段里加注释了 这里不多说了 比较基础的

看一下返回的数据 两种查询方式返回的数据都是一致:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值