Java查询数据库所有数据之后,在程序中递归获取所有parent节点下子节点,
如下代码:
public static List<Integer> getAllNewsClass(List<Menu> menuList, int pid,List<Integer> childList) {
for (Menu mu : menuList) {
//遍历出父id等于参数的id,add进子节点集合
if (Integer.valueOf(mu.getParentId()) == pid) {
childList.add(mu.getId());
//递归遍历下一级
getAllNewsClass(menuList, mu.getId(),childList);
}
}
return childList;
}