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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM