/** 加载菜单 */ @PostConstruct public void cacheMenus() throws Exception { try { List<MenuVo> menuList = DBUtils.jt() .query("select * from T_MENU order by displayOrder", createRowMapper(MenuVo.class)); // 形成树状关系 Map<String, MenuVo> menuMap = new HashMap<String, MenuVo>(); for (MenuVo menu : menuList) { menuMap.put(menu.getMenuId(), menu); } for (MenuVo menu : menuList) { String pid = menu.getParentMenuId(); if (!StringUtils.isEmpty(pid)) { MenuVo pMenu = menuMap.get(pid); pMenu.addSubMenu(menu); } } for (MenuVo menu : menuList) { if (!StringUtils.isEmpty(menu.getParentMenuId())) { menuMap.remove(menu.getMenuId()); } } LocalCache.put(CACHE_KEY_MENU_MAP, Collections.unmodifiableMap(menuMap)); logger.info("菜单缓存构建完毕。"); } catch (Exception e) { logger.error("加载菜单出错", e); throw e; } }
/** 获取菜单,并根据当前登录用户的权限设置进行过滤 */ public MenuVo getMenu(String menuId) throws Exception { @SuppressWarnings("unchecked") Map<String, MenuVo> menuMap = (Map<String, MenuVo>) LocalCache.get(CACHE_KEY_MENU_MAP); MenuVo tempMenu = menuMap.get(menuId); return filter(tempMenu.clone()); }