方法一:
/*建立树形结构*/
public List<DeptSearchVo> builTree(){
List<DeptSearchVo> treeMenus =new ArrayList<DeptSearchVo>();
for(DeptSearchVo menuNode : getRootNode()) {
menuNode=buildChilTree(menuNode);
treeMenus.add(menuNode);
}
return treeMenus;
}
/*递归,建立子树形结构*/
private DeptSearchVo buildChilTree(DeptSearchVo pNode){
List<DeptSearchVo> chilMenus =new ArrayList<DeptSearchVo>();
for(DeptSearchVo menuNode : menuList) {
if(menuNode.getParentId().equals(pNode.getId())) {
chilMenus.add(buildChilTree(menuNode));
}
}
pNode.setChildren(chilMenus);
return pNode;
}
/*获取根节点*/
private List<DeptSearchVo> getRootNode() {
List<DeptSearchVo> rootMenuLists =new ArrayList<DeptSearchVo>();
for(DeptSearchVo menuNode : menuList) {
if(menuNode.getParentId().equals("0")) {
rootMenuLists.add(menuNode);
}
}
return rootMenuLists;
}
方法二:
private List<FuncConfigVo> buildChilTree(String parentId){
List<FuncConfigVo> list = funcConfigMapper.getFuncParentId(parentId);
for(FuncConfigVo funcConfigVo : list) {
String id = funcConfigVo.getId();
List<FuncConfigVo> child = buildChilTree(id);
if(!child.isEmpty()) {
funcConfigVo.setChildren(child);
}
}
return list;
}
原文:https://www.cnblogs.com/yyhhblog/p/14121391.html