java生成树形结构

public R treeNode() {
//1.查询1级菜单
List<System> systemList = systemService.list(new QueryWrapper<System>().eq("parent_id", 0));
List<System> list = systemService.list(); //查询全部
List<System> listNodes = new ArrayList<>();
for (System system : systemList) {
System listNode = findListNode(list, system);
listNodes.add(listNode);
}
return R.data(listNodes);
}


/**
* @param list 所有元素
* @param sys 父节点
* @return 树状jsonNode
*/
private System findListNode(List<System> list, System sys) {
List<System> systemList = new ArrayList<>();
for (System system : list) {
if (sys.getId().equals(system.getParentId())) {
systemList.add(findListNode(list, system));
}
}
sys.setChildren(systemList);
return sys;
}

上一篇:oracle调整列顺序


下一篇:python之sys实战、hashlib实战、异常、面向对象