数据库表明是Menu,数据库表结构很简单 id,parent_id 。就这样 下面是java代码。
public List<Map<String,Object>> getChild(String id,List<Menu> allList){
List<Map<String,Object>> childList = new ArrayList<>();
for(Menu menu : allList){
if(id.equals(menu.getParent_id())){
Map<String,Object> map = new HashMap<>();
map.put("ID",menu.getId());
map.put("PARENT_ID",menu.getParent_id());
map.put("HASCHILDREN",FALSE);
map.put("CHILDREN",new Object[]{});
childList.add(map);
}
}
for(Map<String,Object> map : childList){
List<Map<String,Object>> tList = getChild(String.valueOf(map.get("id")),allList);
if(!tList.isEmpty()){
map.put("HASCHILDREN",TRUE);
}
map.put("CHILDREN",tList);
}
return childList;
}