public List<SysMenu> getSysMenuByRoleId(Integer roleId) throws Exception { try { ArrayList<SysMenu> tempList = new ArrayList<>(); List<SysMenu> smList = sysMenuMapper.getSysMenuByRoleId(roleId); for (SysMenu sysMenu : smList) { //判断找出父节点 if (sysMenu.getParentId() == null) { //把所有父节点保存到一起 tempList.add(sysMenu); } else { //遍历所有的父节点 for (SysMenu menu : tempList) { if (menu.getId().equals(sysMenu.getParentId())) { //找出子节点对应的父节点,然后填充 List<SysMenu> children = menu.getChildren(); if (children == null) children = new ArrayList<>(); children.add(sysMenu); menu.setChildren(children); } } } } smList.sort(Comparator.comparing(SysMenu::getId)); return smList; } catch (Exception e) { log.error("【根据RoleId获取SysMenu数据异常】异常信息为:" + e.getMessage()); throw new Exception(e.getMessage()); } }
原文:https://www.cnblogs.com/coding8832/p/14470852.html