service层递归方法查询指定类别下的所有子类别

来源:互联网 发布:于丹 知乎 编辑:程序博客网 时间:2024/05/22 00:34

service层递归方法查询指定类别下的所有子类别

  1. 查询指定类别下的直接子类别
  2. 用递归的方式根据给定类别查找所有子类别

以下是代码

/**     * 查询该类别下的直系所有类别     *     * @param parentId     * @return     */    public List<Category> getCategoryListByParentId(String parentId) {        String hql = "from Category  where parent.id=? and status <> -1 order by createTime desc";        return categoryDao.find(hql, parentId);    }    /**     * 根据给定类别查找所有子类别     *     * @param topChildren     * @param resultChildren     * @return     */    public List<Category> getAll(List<Category> topChildren, List<Category> resultChildren) {        for (Category c : topChildren                ) {            resultChildren.add(c);            if (getCategoryListByParentId(c.getId()) == null) {//没有叶子节点                continue;            }            List<Category> categories = getCategoryListByParentId(c.getId());            getAll(categories, resultChildren);        }        return resultChildren;    }    /**     * 查看当前类别下的所有类别(递归)     *     * @param parentId     * @return     */    public List<Category> getCategoryList(String parentId) {        List<Category> categoryList = new LinkedList<>();        List<Category> topChildren = getCategoryListByParentId(parentId);        getAll(topChildren, categoryList);        return categoryList;    }
阅读全文
0 0