通过登录人userId获取对应角色 再获取对应角色下的权限菜单(用户角色关联表,角色菜单关联表)1,构建递归获取父节点方法 (因为角色菜单关联表中保存的只有菜单的叶子节点 所以要递归获取父节点) private void getUserMenus(HashMapparentAndChildren, SysMenu sysMenu) { if (!parentAndChildren.containsKey(sysMenu.getRowId())) { parentAndChildren.put(sysMenu.getRowId(), sysMenu); } if (!parentAndChildren.containsKey(sysMenu.getParentId())) { SysMenu p = sysMenuMapper.find(sysMenu.getParentId()); if (p != null) { getUserMenus(parentAndChildren, p); } } }2,递归获取子节点private void getTreeStructure(SysMenuDto root, List orgs) { for (SysMenu org : orgs) { if (org.getParentId().equals(root.getId())) { SysMenuDto s = SysMenuDto.convertSysMenuEntity(org); getTreeStructure(s, orgs); if (root.getChildrens() == null) { root.setChildrens(new ArrayList<>()); } root.getChildrens().add(s); } } }3,构建树形数据结构 public List getSysMenuTreeDataByAdmin(String userId,String did) { List list = new ArrayList<>(); //查询所有的菜单列表 List sysMenuList = sysRoleMapper.findListVisible(); //查询所有的菜单编码列表 List sysMenuIdList = sysRoleMapper.findListId(); //获取收藏的菜单 List sysUserFavoriteMenuList = sysUserFavoriteMenuMapper.getSysUserFavoriteMenu(userId, did); //遍历判断是否是收藏的 for(SysMenu sysMenu : sysMenuList){ SysUserFavoriteMenu sysUserFavoriteMenu = sysUserFavoriteMenuMapper.getFavoriteMenu(userId,did,sysMenu.getRowId()); if(sysUserFavoriteMenu != null){ //是否收藏标志设置为收藏 sysMenu.setIsFavorite("1"); }else{ sysMenu.setIsFavorite("0"); } } for(SysUserFavoriteMenu sysUserFavoriteMenu : sysUserFavoriteMenuList){ //判断收藏的菜单在所有的菜单中是否存在 不存在就把这个用户收藏的菜单移除 if(!sysMenuIdList.contains(sysUserFavoriteMenu.getMenuId())){ sysUserFavoriteMenuMapper.deleteFavoriteMenu(sysUserFavoriteMenu.getMenuId()); } } //获取该经销商默认设置的系统菜单版本 String verdion=null; //获取顶级菜单 List topMenuList = sysRoleMapper.selectTopMenu(verdion); if (null == topMenuList) { return null; } for (SysMenu menu : topMenuList) { SysMenuDto s = SysMenuDto.convertSysMenuEntity(menu); getTreeStructure(s, sysMenuList); list.add(s); } //排序 list.sort((SysMenuDto s1,SysMenuDto s2) -> s1.getSort() >= s2.getSort() ? 1 : -1); logger.info("获取最终菜单:{}", JsonUtil.obj2String(list)); return list; }