Java递归查询树形列表。
需求是前端需要树形结构的选择器,所以需要给前端提供树形结构的数据
直接贴代码!!!
@RequestMapping(method = RequestMethod.POST, value = "/queryTreeList")
public void queryTreeList(HttpServletRequest request,HttpServletResponse response,@RequestBody Map param){
try {
List<Map> res = new ArrayList<>();
QueryWrapper<BuiSyFunction> wrapper = initWrapper(param);
wrapper.eq("state",1);
wrapper.eq("function_parent_id",0);
//查询所有一级菜单
List<BuiSyFunction> functions = buiSyFunctionService.list(wrapper);
// List<Object> list = new ArrayList<>();
//遍历一级菜单
for (BuiSyFunction function : functions){
Map map = new HashMap();
Long id = function.getId();
map.put("value",id);
map.put("label",function.getFunctionName());
//递归查询子级菜单
List<Object> childFunctions = getChildFunctions(id);
if (childFunctions.size() > 0){
map.put("children",childFunctions);
}
res.add(map);
}
renderResult(response,res);
}catch (Exception e){
e.printStackTrace();
}
}
public List<Object> getChildFunctions(Long parentId){
List<Object> res = new ArrayList<>();
QueryWrapper<BuiSyFunction> wrapper = new QueryWrapper<>();
wrapper.eq("state",1);
wrapper.eq("function_parent_id",parentId);
//根据父Id查询菜单
List<BuiSyFunction> functions = buiSyFunctionService.list(wrapper);
for(BuiSyFunction function : functions){
Map map = new HashMap();
Long id = function.getId();
map.put("value",id);
map.put("label",function.getFunctionName());
//递归查询子级菜单
List<Object> childFunctions = getChildFunctions(id);
if (childFunctions.size() > 0){
map.put("children",childFunctions);
}
res.add(map);
}
return res;
}