mybatis+mysql查询类别下的所有子类别(递归)

4 篇文章 0 订阅

转自:https://blog.csdn.net/menglinjie/article/details/78676117

场景:
1. 查询所有类别(大类别+子类别+子类别下的所有类别..);
2. 查询某个类别下的所有子类别;
3. 递归查询;
亲测可用,亲手总结!

实体类(省略get set方法)
public class Category implements Serializable {

    private int id;
    private String cname;
    private int count;//类别数量
    private List<Category> categoryList;//子类类别集合

   // private List<Book> bookList;//类别下的图书集合
}

dao层
当id=0时查找所有类别,id不为0时查找id下的自类别

public interface CategoryDao {

    //查看所有类别(id=0)(或指定类别下所有子类别)
    public List<Category> getAll(@Param("categoryId") int categoryId);

mapper
先根据id查找到大类别,然后根据查找到的类别id递归查找其下的所有子类别

<resultMap id="categoryMap1" type="Category">
        <id property="id" column="id"></id>
        <result property="cname" column="cname"></result>
        <result property="count" column="count"></result>
        <collection property="categoryList" ofType="Category" javaType="java.util.List" column="id" select="getById"/>
    </resultMap>

    <!--根据父类id查找其子类别-->
    <select id="getById" resultMap="categoryMap1" parameterType="int">
        SELECT *
        FROM dcategory
        WHERE parent_id = #{id}
    </select>

    <!--查找所有类别(递归)-->
    <select id="getAll" resultMap="categoryMap1" parameterType="int">
        SELECT *
        FROM dcategory
        WHERE 1 = 1
        <choose>
            <when test="categoryId ==0">
                AND dcategory.parent_id IS NULL
            </when>
            <otherwise>
                AND id = #{categoryId}
            </otherwise>
        </choose>
    </select>

测试
@Test
    public void testGetAll() {
        List<Category> categories = categoryDao.getAll(1);
        System.out.println("测试获取所有类别========》" + categories);

        for (Category c : categories
                ) {
            System.out.println(c.getCname() + "=========" + c.getCount());
            List<Category> childs = c.getCategoryList();
            for (Category c1 : childs
                    ) {
                System.out.println(c1.getCname());
            }

        }
    }
 

--------------------- 
作者:孟林洁 
来源:CSDN 
原文:https://blog.csdn.net/menglinjie/article/details/78676117 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

1.java+js递归循环树实现无限下拉菜单:https://blog.csdn.net/lianzhang861/article/details/83783796

2.Mybatis递归查询并自动装填所有子节点:https://blog.csdn.net/lianzhang861/article/details/86243532

3. mybatis+mysql查询类别下的所有子类别(递归):https://blog.csdn.net/menglinjie/article/details/78676117

4.Mybatis自查询递归查找子菜单:https://blog.csdn.net/frankcheng5143/article/details/72723958

5. Mybatis实现部门递归查询: https://blog.csdn.net/qq_37342374/article/details/79643291

6. Mybatis之高级映射collection (递归查出树形数据之查询部门及部门下所有人员 单节点):https://blog.csdn.net/FANTASY522272820/article/details/70053449

7.MyBatis 关联关系查询 自关联 一对多查询指定父节点的所有子节点:https://www.2cto.com/kf/201704/625280.html

8. mybatis递归查询所有子节点: https://blog.csdn.net/QQ_1094428625/article/details/82767340

9. mysql -- 递归查询所有子节点:https://blog.csdn.net/bob_baobao/article/details/78104504

10. mysql查询所有子节点,后代: https://blog.csdn.net/xiaofanren1111/article/details/79711774

11. Mybatis中实现递归查询: https://blog.csdn.net/janet796/article/details/79500349

12.查询树节点以及所有子节点https://sshitaime.iteye.com/blog/2019796

13. mysql树节点【所有子节点列表 and 查询所有父节点列表】:https://blog.csdn.net/u014033756/article/details/70567746

14.mybatis+mysql递归查询:https://blog.csdn.net/make_a_difference/article/details/51778233

15.mysql中树状表的所有子节点递归查询和所有父节点递归查询方法: https://blog.csdn.net/abc286229825/article/details/78905673

16.MyBatis中动态SQL语句完成多条件查询:https://blog.csdn.net/qq_21454973/article/details/78096832

17.mybatis如何实现递归查询:https://bbs.csdn.net/topics/392208379?page=1

18.Mybatis之高级映射collection :https://blog.csdn.net/u014774073/article/details/78119954

递归SQL:

    WITH RECURSIVE r AS (
    SELECT * FROM m_am WHERE m.attri_id = 346
       union  ALL
    SELECT m1.* FROM m_am1, r WHERE m1.attri_pid = r.attri_id
    )
    SELECT r.attri_id FROM r ORDER BY attri_id;
        
        
        WITH RECURSIVE r AS (
        SELECT * FROM ml m where m.ml_id = 1
           union all 
             SELECT m1.* from ml m1 , r where m1.ml_pid = r.ml_id
        
        )
        SELECT r.*    from r ORDER BY ml_id;

 

<select id="findByModelIds" parameterType="java.util.List" resultMap="BaseResultMap">
   WITH RECURSIVE r AS (
  SELECT * FROM model_attribute m
  where m.model_id in
  <foreach item="list" collection="list" open="(" separator="," close=")">
    #{list}
  </foreach>
  union  ALL
  SELECT m1.* FROM model_attribute m1, r WHERE m1.attri_pid = r.attri_id
  )
  SELECT r.* FROM r ORDER BY attri_id
</select>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用MyBatis递归查询实现树形结构的查询。具体步骤如下: 1. 定义实体类 定义一个实体类,表示树的节点,包含节点id、父节点id、节点名称等属性。 2. 定义Mapper接口 定义一个Mapper接口,包含一个方法,用于查询树形结构的节点。方法的返回值为List集合,表示查询到的树形结构的节点。 3. 编写Mapper.xml配置文件 在Mapper.xml配置文件中,编写递归查询语句,通过查询语句实现树形结构的查询。具体实现方式如下: - 定义一个select语句,用于查询指定节点的所有节点。 - 在select语句中,使用union all关键字连接多个查询语句,实现递归查询。 - 在查询语句中,使用with recursive关键字定义递归查询语句。 4. 调用Mapper接口 在Java代码中,调用Mapper接口的方法,获取查询到的树形结构的节点。可以通过递归遍历节点,实现树形结构的展示。 下面是一个示例代码,供参考: ``` // 定义实体类 public class TreeNode { private Integer id; private Integer parentId; private String name; // getter和setter方法 } // 定义Mapper接口 public interface TreeNodeMapper { List<TreeNode> selectTreeNodes(Integer parentId); } // 编写Mapper.xml配置文件 <select id="selectTreeNodes" parameterType="java.lang.Integer" resultType="TreeNode"> with recursive cte(id, parent_id, name) as ( select id, parent_id, name from tree_node where parent_id = #{parentId} union all select tn.id, tn.parent_id, tn.name from tree_node tn inner join cte on tn.parent_id = cte.id ) select * from cte; </select> // 调用Mapper接口 @Autowired private TreeNodeMapper treeNodeMapper; public List<TreeNode> getTreeNodes(Integer parentId) { return treeNodeMapper.selectTreeNodes(parentId); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值