controller:
@RequestMapping("/list/tree") public R list(){ List<CategoryEntity> list = categoryService.listWithTree(); return R.ok().put("data", list); }
service.impl
@Override public List<CategoryEntity> listWithTree() { List<CategoryEntity> all = baseMapper.selectList(null); List<CategoryEntity> list = all.stream().filter((categoryEntity -> categoryEntity.getParentCid()==0)) .map((root)->{ root.setChildList(getChildList(root, all)); return root; }).sorted((menu1,menu2)->{ return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort()); }).collect(Collectors.toList()); return list; } //递归查找 private List<CategoryEntity> getChildList(CategoryEntity root, List<CategoryEntity> all) { List<CategoryEntity>