java查询几个菜单下的所有下级菜单

需求:

  假如有几个一级菜单,一级菜单下面有几个二级菜单,二级菜单下又还有三级菜单。现在要求一级菜单里面的几个设置为无效,将不显示在前端。现在需要的是查询出一级菜单下面所有的菜单,包括二级,三级菜单

原则:

  在菜单表中包括自己的id和父节点的parentId

代码:

 1 public List<Map> getAllMappings(){
 2         //从数据库查出需要设置为无效的一级菜单,每个Map包含id,parentId,name等字段 
4
List<Map> notActiveMenus = (List<Map>)getMenuDao().search();
      //初始化存储所有无效的菜单的List
5 List<String> notActiveIds = new ArrayList<String>();
      //循环每一个一级菜单
6 for (Map notActiveMenu:notActiveMenus){ 7 notActiveIds.add((String)notActiveMenu.get("id"));
        //遍历下级目录
8 searchSubNotActiveMenu(notActiveIds, (String) notActiveMenu.get("id")); 9 }
      //去重
10 List<String> temp = new ArrayList<String>(); 11 Iterator<String> it = notActiveIds.iterator(); 12 while(it.hasNext()){ 13 String tempString = it.next(); 14 if(temp.contains(tempString)){ 15 it.remove(); 16 }else { 17 temp.add(tempString); 18 } 19 } 20 27 return notActiveIds; 28 } 29 30 public void searchSubNotActiveMenu(List<String> notActiveIds,String parentId){ 31 //将上级目录的id作为父节点查询child菜单33
       List<Map> datas = (List<Map>)getMenuDao().search(parentId); 34 if (datas!=null&&datas.size()>0){ 35 for (Map data:datas){ 36 notActiveIds.add((String)data.get("id")); 37 searchSubNotActiveMenu(notActiveIds,(String)data.get("id")); 38 } 39 } 40 }

 

posted @ 2017-07-27 19:51  YzdFly  阅读(4503)  评论(0编辑  收藏  举报