mybatis如何实现查询一级目录下的二级,三级菜单的两种方法

数据库如图

新开一个实习类categoryVo在原来ctegory的基础上加上一个数组

查询一级目录下的二三级目录

方法一:连接查询

<resultMap id="categoryVoMap" type="com.qfedu.fmmall.entity.CategoryVo">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="category_id1" jdbcType="INTEGER" property="categoryId" />
    <result column="category_name1" jdbcType="VARCHAR" property="categoryName" />
    <result column="category_level1" jdbcType="INTEGER" property="categoryLevel" />
    <result column="parent_id1" jdbcType="INTEGER" property="parentId" />
    <result column="category_icon1" jdbcType="VARCHAR" property="categoryIcon" />
    <result column="category_slogan1" jdbcType="VARCHAR" property="categorySlogan" />
    <result column="category_pic1" jdbcType="VARCHAR" property="categoryPic" />
    <result column="category_bg_color1" jdbcType="VARCHAR" property="categoryBgColor" />
    <collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVo">
      <id column="category_id2" jdbcType="INTEGER" property="categoryId" />
      <result column="category_name2" jdbcType="VARCHAR" property="categoryName" />
      <result column="category_level2" jdbcType="INTEGER" property="categoryLevel" />
      <result column="parent_id2" jdbcType="INTEGER" property="parentId" />
      <collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVo">
        <id column="category_id3" jdbcType="INTEGER" property="categoryId" />
        <result column="category_name3" jdbcType="VARCHAR" property="categoryName" />
        <result column="category_level3" jdbcType="INTEGER" property="categoryLevel" />
        <result column="parent_id3" jdbcType="INTEGER" property="parentId" />
      </collection>
    </collection>
  </resultMap>

  <select id="selectAllCategories" resultMap="categoryVoMap">
    select c1.category_id       'category_id1',
           c1.category_name     'category_name1',
           c1.category_level    'category_level1',
           c1.parent_id         'parent_id1',
           c1.category_icon     'category_icon1',
           c1.category_slogan   'category_slogan1',
           c1.category_pic      'category_pic1',
           c1.category_bg_color 'category_bg_color1',
           c2.category_id       'category_id2',
           c2.category_name     'category_name2',
           c2.category_level    'category_level2',
           c2.parent_id         'parent_id2',
           c3.category_id       'category_id3',
           c3.category_name     'category_name3',
           c3.category_level    'category_level3',
           c3.parent_id         'parent_id3'
    from category c1
           inner join category c2
                      on c2.parent_id = c1.category_id
           left join category c3
                     on c3.parent_id = c2.category_id
    where c1.category_level = 1
  </select>

方法二:子查询

定义mapper包下的接口

代码如下

<resultMap id="categoryVoMap2" type="com.qfedu.fmmall.entity.CategoryVo">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="category_id" jdbcType="INTEGER" property="categoryId" />
        <result column="category_name" jdbcType="VARCHAR" property="categoryName" />
        <result column="category_level" jdbcType="INTEGER" property="categoryLevel" />
        <result column="parent_id" jdbcType="INTEGER" property="parentId" />
        <result column="category_icon" jdbcType="VARCHAR" property="categoryIcon" />
        <result column="category_slogan" jdbcType="VARCHAR" property="categorySlogan" />
        <result column="category_pic" jdbcType="VARCHAR" property="categoryPic" />
        <result column="category_bg_color" jdbcType="VARCHAR" property="categoryBgColor" />
        <collection property="categories" column="category_id" select="selectAllCategories2"></collection>
        <!--拿到每个一级分类category_id查询它的下一级信息,相当于递归调用-->
    </resultMap>

    <!--根据父级分类id查询子集分类-->
    <select id="selectAllCategories2" resultMap="categoryVoMap2" >
        select
            category_id,
            category_name,
            category_level,
            parent_id,
            category_icon,
            category_slogan,
            category_pic,
            category_bg_color
        from category where parent_id = #{parendId}
    </select>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
关于 Mybatis 多层级联查询的问题,可以使用 Mybatis 的一些高级特性来解决,例如使用嵌套查询或者使用 Mybatis 提供的 resultMap 来实现多层级联查询。 嵌套查询,是指在一个查询语句中嵌套另外一个查询语句。在 Mybatis 中,可以使用 <select> 标签来定义一个查询语句,然后在另一个 <select> 标签中嵌套这个查询语句,以此实现多层级联查询。 resultMap,是 Mybatis 中用来映射查询结果集的一个重要特性。通过在 resultMap 中定义一个 <association> 标签或者 <collection> 标签,可以实现多层级联查询的功能。其中,<association> 标签用于表示一对一关系,<collection> 标签用于表示一对多关系。 例如,假设有三个表 A、B、C,它们的关系是 A 和 B 一对一,B 和 C 一对多,可以通过下面的方式实现多层级联查询: ``` <!-- 定义 A 表的 resultMap --> <resultMap id="AResultMap" type="A"> <id property="id" column="id"/> <result property="name" column="name"/> <association property="b" resultMap="BResultMap"/> </resultMap> <!-- 定义 B 表的 resultMap --> <resultMap id="BResultMap" type="B"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="cs" resultMap="CResultMap"/> </resultMap> <!-- 定义 C 表的 resultMap --> <resultMap id="CResultMap" type="C"> <id property="id" column="id"/> <result property="name" column="name"/> </resultMap> <!-- 定义查询语句 --> <select id="queryA" resultMap="AResultMap"> select a.id, a.name, b.id as b_id, b.name as b_name, c.id as c_id, c.name as c_name from A a left join B b on a.id = b.a_id left join C c on b.id = c.b_id </select> ``` 在这个例子中,通过在 A 表的 resultMap 中定义了一个 <association> 标签,将 A 表和 B 表关联起来。然后在 B 表的 resultMap 中定义了一个 <collection> 标签,将 B 表和 C 表关联起来。最后在查询语句中通过左连接将三个表连接起来,就可以实现多层级联查询了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啥也不会hh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值