JAVA 递归实现权限菜单树

实体类

@Data
public class Menu implements Serializable {
    private String  id;
    private String name;
    private String pid;
    private List<Menu> children;
}

控制层

@RestController
@RequestMapping("menu")
public class MenuController {

	@Autowired
    private MenuService menuService ;

	@RequestMapping("/tree")
    public List<Menu> tree(){
        List<Menu> menusList = menuService.getMenuByPid(0);
        List<Menu> menuLNotList = menuService.getMenuByNotPid();
        for (Menu menu : menusList) {
            List<Menu> menus = iterateMenuList(menuLNotList, menu.getId());
            menu.setChildren(menus);
        }
        return  menusBase;
    }

	
    public List<Menu> iterateMenuList(List<Menu> menuLNotList, String pid){
        List<Menu> result = new ArrayList<Menu>();
        for (Menu menu : menuLNotList) {
            String menuid = menu.getId();
            String parentid = menu.getPid();
            if(!StringUtils.isBlank(parentid)){
                if(parentid.equals(pid)){
                    List<Menu> iterateMenu = iterateMenuList(menuLNotList, menuid);
                    menu.setChildren(iterateMenu);
                    result.add(menu);
                }
            }
        }
        return result;
    }
}

你可能感兴趣的:(java,开发语言)