本次读取数据只是依照我项目逻辑
1、先获取所有菜单
List list=baseMenuService.getPowerTree(ParentMenuId);
2、在从菜单中得到所有一级菜单
List rootMenu=new ArrayList<>();
for (int i=0;i<list.size();i++){
if (list.get(i).getParentmenuid()==0){
rootMenu.add(dealMenuItem(list.get(i)));
}
}
3、循环一级菜单,对每个一级菜单进行递归
for (BaseMenu menu:rootMenu){
menu.setChildren(getChild(menu.getId(),list));
}
4、递归方法
/**
* 递归菜单
*/
private List getChild(Integer id,List allList){
//子菜单
List childList=new ArrayList<>();
for (BaseMenu menu:allList){
if (menu.getParentmenuid().equals(id)){
childList.add(dealMenuItem(menu));
}
}
//把子菜单的子菜单在循环一遍
for (BaseMenu menu:childList){
menu.setChildren(getChild(menu.getId(),allList));
}
//递归退出条件
if (childList.size()==0){
return null;
}
return childList;
}
private BaseMenu dealMenuItem(BaseMenu menu){
JSONArray jsonArray=JSONArray.parseArray(menu.getMenupower());
List<BasePower> powers=new ArrayList<>();
if (jsonArray.isEmpty()){
jsonArray=JSONArray.parseArray("[{"PowerId":7,"PowerName":"查看"}]");
}
for (int i=0;i<jsonArray.size();i++){
JSONObject object=jsonArray.getJSONObject(i);
BasePower basePower=new BasePower();
basePower.setPowerid(object.getInteger("PowerId"));
basePower.setPowername(object.getString("PowerName"));
powers.add(basePower);
menu.setPower(powers);
}
return menu;
}