1. 程式人生 > >遞迴遍歷樹結構-已解決

遞迴遍歷樹結構-已解決

在專案中用到導航樹結構,所以就用遞迴寫了一個遍歷導航樹的功能。

表結構:


/**
	 * 遞迴獲取選單
	 * 
	 * @param roleKey
	 * @param systemCode
	 * @return
	 */
	public String getSysMenuJson(String roleIds, String menu_pid)
			throws Exception {
		List<Map<String, Object>> list = null;
		if (roleIds == null || roleIds.equals(""))
			return null;
		String[] roles = roleIds.split(",");
		List<Map<String, Object>> dicList = new ArrayList<Map<String, Object>>();
		//當前節點資訊
		//dicList = SysMenuListById(roleIds, menu_pid);
		//呼叫遞迴子集節點資訊
		getChridNavIds(roleIds, menu_pid, dicList);
		return JSONArray.fromObject(dicList).toString();
	}
	/**
	 * 遞迴獲取系統導航選單
	 * @param roleIds   角色iD
	 * @param parentid  父級選單ID
	 * @param dicList   
	 */
	private void getChridNavIds(String roleIds, String parentid,List dicList){
		List<Map<String, Object>>  dataDictionaryList= SysMenuListByPid(roleIds, parentid);
		
		for(Map nav:dataDictionaryList){
			List<Map<String, Object>>  childList = new ArrayList<Map<String, Object>> ();
			if(nav!=null){
				Map<String, Object> map = new HashMap<String, Object>();// 每個一級選單對應的map
				map.put("id", NullHelper.convertNullToNothingnull(nav.get("menu_id")));
				map.put("name", NullHelper.convertNullToNothingnull(nav.get("menu_name")));
				String url = NullHelper.convertNullToNothingnull(nav.get("menu_url"));
				if(StringUtils.isNotBlank(url)){
					map.put("url", url);
				}
				map.put("icon", NullHelper.convertNullToNothingnull(nav.get("menu_img")));
				getChridNavIds(roleIds,nav.get("menu_id").toString(),childList);
				//將同級程式碼存入childList中
				map.put("menuitem", childList);
				dicList.add(map);
			}
		}
	}
	
	/**
	 * 用PID遞迴獲取父級選單的所有子集選單
	 * @param roleIds
	 * @param menu_pid
	 * @param bResult
	 * @return
	 */
	private List<Map<String, Object>> SysMenuListByPid(String roleIds, String menu_pid) {
		StringBuffer menuSql = new StringBuffer();
		menuSql.append("select m.menu_id,m.parent_menu_id,m.menu_name,m.menu_url,m.menu_img,m.is_treenode from sys_menu m ");
		menuSql.append("inner join sys_role_menu_rel rm on rm.menu_id=m.menu_id and m.parent_menu_id in ("+splitRoleId(menu_pid)+") and m.menu_state='1' and rm.role_id in ("+splitRoleId(roleIds)+") ");
		menuSql.append("where 1=1" + " group by m.menu_id,m.parent_menu_id,m.menu_name,m.menu_url,m.menu_img,m.is_treenode,m.menu_seq ");
		menuSql.append("order by m.menu_seq asc ");
		return jdbcTemplate.queryForList(menuSql.toString());
	}

生成json串:

[
    {
        "id": "8a929b985da6b83f015da6bbe2620001", 
        "icon": "s-n-t-icon5.png", 
        "name": "制證管理", 
        "menuitem": [
            {
                "id": "8a92e1475da5c3a3015da80915080007", 
                "icon": "", 
                "name": "證照生成", 
                "menuitem": [ ], 
                "url": "license/manageList"
            }, 
            {
                "id": "8a92e1475dbc8d86015dbc91ed990002", 
                "icon": "", 
                "name": "證照稽核", 
                "menuitem": [ ], 
                "url": "storage/auditList"
            }, 
            {
                "id": "8a929cee5da76049015da764fd980001", 
                "icon": "", 
                "name": "制證簽發", 
                "menuitem": [ ], 
                "url": "storage/signDataList"
            }, 
            {
                "id": "8a929cee5da76049015da7743ae600a0", 
                "icon": "", 
                "name": "打證管理", 
                "menuitem": [ ], 
                "url": "storage/printDataList"
            }
        ], 
        "url": "license/manageList"
    }, 
    {
        "id": "8a929b1a5da632c3015da6347edd0001", 
        "icon": "s-n-t-icon2.png", 
        "name": "證照維護", 
        "menuitem": [
            {
                "id": "8a92e3fd5dea5abd015dea6e6f7b0001", 
                "icon": "", 
                "name": "證照年檢", 
                "menuitem": [ ], 
                "url": "licensemaintain/licenseAnnualSurveyList"
            }, 
            {
                "id": "8a929b1a5da632c3015da6368e5a002b", 
                "icon": "", 
                "name": "證照變更", 
                "menuitem": [ ], 
                "url": "licensemaintain/licenseChangeList"
            }, 
            {
                "id": "297e4d0b5dbb4a09015dbc25eb7c0003", 
                "icon": "", 
                "name": "證照登出", 
                "menuitem": [ ], 
                "url": "licensemaintain/licensezhuxiao"
            }, 
            {
                "id": "297e4d0b5dbb4a09015dbc2defb700a4", 
                "icon": "", 
                "name": "證照作廢", 
                "menuitem": [ ], 
                "url": "licensemaintain/licensezuofei"
            }, 
            {
                "id": "297e4d0b5dbb4a09015dbc3122e500f6", 
                "icon": "", 
                "name": "證照掛失", 
                "menuitem": [ ], 
                "url": "licensemaintain/licenseguashi"
            }, 
            {
                "id": "297e4d0b5dbb4a09015dbc32dbc60149", 
                "icon": "", 
                "name": "維護稽核", 
                "menuitem": [ ], 
                "url": "licensemaintain/licenseshenhe"
            }, 
            {
                "id": "8a92e0f65dc5c003015dc5c58ca60001", 
                "icon": "", 
                "name": "版本管理", 
                "menuitem": [ ], 
                "url": "licensemaintain/versionManagement"
            }, 
            {
                "id": "8a92e0f65dc5c003015dc5c88df30002", 
                "icon": "", 
                "name": "有效期管理", 
                "menuitem": [ ], 
                "url": "licensemaintain/termOfValidityManagement"
            }
        ]
    }, 
    {
        "id": "8a92e3165e177bec015e1786a9a20002", 
        "icon": "s-n-t-icon1.png", 
        "name": "證照查詢", 
        "menuitem": [ ], 
        "url": "querylicenses/querManageList"
    }
]