MySQL根据父节点id查询所有子节点

最近写一个项目,遇到一个问题需要传进去一个id查出这张表下面所有的子节点(只有一张表),当时想着就是写一条sql查出来,sql采用递归的方式实现,可是试了半天还是不对,于是我在百度上查了一下还真有方法,
下来我把链接放上去,方便下次查看:
https://www.csdn.net/gather_22/MtTaIgysMDE5Mi1ibG9n.html
返回查出来的是集合 然后采用递归的方式 就可以变成树形菜单了。

以下是我写的sql语句:

select * from (
              select t1.*, 
              if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', company_menu_model_id), 0) as ischild
              from (
                   select * from company_menu_model t order by t.company_menu_model_id asc
                  ) t1,
                  (select @pids := #{menuId}) t2
             ) t3 where ischild != 0
 @GetMapping("/buildTreeMenu/{menuId}")
    public NewApiResult<TreeSelect> buildTreeMenu(@PathVariable Long menuId)
    {
        List<CompanyMenuModel> list = CompanyMenuService.getBuildList(menuId);
        if(StringUtils.isEmpty(list)){

            return NewApiResult.error("该菜单没有子菜单");
        }

        return NewApiResult.success(CompanyMenuService.buildTree(list,menuId));
    }

下边是树形菜单封装的过程:

 /**
     * 获取树形菜单
     *
     * @param list
     * @return
     */
    @Override
    public List<CompanyMenuModel> buildTree(List<CompanyMenuModel> list,Long menuId) {
        List<CompanyMenuModel> returnList = new ArrayList<CompanyMenuModel>();
        for (Iterator<CompanyMenuModel> iterator = list.iterator(); iterator.hasNext(); ) {
            CompanyMenuModel t = (CompanyMenuModel) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == menuId) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty()) {
            returnList = list;
        }
        return returnList;
    }
  /**
     * 递归
     */
    private void recursionFn(List<CompanyMenuModel> list, CompanyMenuModel t) {
        // 得到子节点列表
        List<CompanyMenuModel> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CompanyMenuModel tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<CompanyMenuModel> it = childList.iterator();
                while (it.hasNext()) {
                    CompanyMenuModel n = (CompanyMenuModel) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<CompanyMenuModel> getChildList(List<CompanyMenuModel> list, CompanyMenuModel t) {
        List<CompanyMenuModel> tlist = new ArrayList<CompanyMenuModel>();
        Iterator<CompanyMenuModel> it = list.iterator();
        while (it.hasNext()) {
            CompanyMenuModel n = (CompanyMenuModel) it.next();
            if (n.getParentId().longValue() == t.getCompanyMenuModelId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<CompanyMenuModel> list, CompanyMenuModel t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

以上就是我写的代码 都是关键的代码,sql的那块上面那个链接上讲的很详细…


版权声明:本文为wojiaohb原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。