Java recursive query tree list.
The requirement is that the front end needs a tree selector, so it needs to provide the front end with tree data
Post code directly!!
@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);
// Query all level-1 menus
List<BuiSyFunction> functions = buiSyFunctionService.list(wrapper);
// List
// Iterate through the first level menu
for (BuiSyFunction function : functions){
Map map = new HashMap();
Long id = function.getId();
map.put("value",id);
map.put("label",function.getFunctionName());
// Recursively query submenus
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);
// Query the menu by parent 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());
// Recursively query submenus
List<Object> childFunctions = getChildFunctions(id);
if (childFunctions.size() > 0){
map.put("children",childFunctions);
}
res.add(map);
}
return res;
}
Copy the code