1.bean(根据需求创建)
@Data public class MenuTreeDto { private String b00; private String parentId; private String b0101; private int level; private Listchildren; }
2.控制层
@RequestMapping("/list/tree") public Result list(){ Listentities = dictService.listWithTree(); return Result.ok(entities); }
3.业务层
@Override public ListlistWithTree() { //1、查出所有分类 List entities = dictService.findAll(); //2、组装成父子的树形结构 //2.1)、找到所有的一级分类 List level1Menus = entities.stream().filter(MenuTreeDto -> MenuTreeDto.getParentId().equals("aaa") ).map((menu)->{ menu.setLevel(1); menu.setChildren(getChildrens(menu,entities)); return menu; }). collect(Collectors.toList()); return level1Menus; }
private ListgetChildrens(MenuTreeDto root,List all){ List children = all.stream().filter(MenuTreeDto -> { return MenuTreeDto.getParentId().equals(root.getB00()); }).map(MenuTreeDto -> { //1、找到子菜单 MenuTreeDto.setLevel(root.getLevel()+1); MenuTreeDto.setChildren(getChildrens(MenuTreeDto,all)); return MenuTreeDto; }) .collect(Collectors.toList()); return children; }
4.postman测试