service层递归方法查询指定类别下的所有子类别
- 查询指定类别下的直接子类别
- 用递归的方式根据给定类别查找所有子类别
以下是代码
/**
* 查询该类别下的直系所有类别
*
* @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;
}