Java实现多级分类查询(树形结构)

Java实现多级分类查询

1、效果预览

{
  "success": true,
  "code": 20000,
  "message": "成功",
  "data": {
    "list": [
      {
        "id": "1450294498352435201",
        "parentId": "0",
        "title": "前端开发",
        "children": [
          {
            "id": "1450294498415349761",
            "parentId": "1450294498352435201",
            "title": "vue",
            "children": []
          },
          {
            "id": "1450294498478264322",
            "parentId": "1450294498352435201",
            "title": "JavaScript",
            "children": []
          },
          {
            "id": "1450294498478264323",
            "parentId": "1450294498352435201",
            "title": "jquery",
            "children": []
          }
        ]
      },
      {
        "id": "1450294498541178882",
        "parentId": "0",
        "title": "后端开发",
        "children": [
          {
            "id": "1450294498541178883",
            "parentId": "1450294498541178882",
            "title": "Java",
            "children": []
          },
          {
            "id": "1450294498608287745",
            "parentId": "1450294498541178882",
            "title": "C++",
            "children": []
          }
        ]
      },
      {
        "id": "1450294498671202305",
        "parentId": "0",
        "title": "数据库开发",
        "children": [
          {
            "id": "1450294498671202306",
            "parentId": "1450294498671202305",
            "title": "mysql",
            "children": [
              {
                "id": "2343243242343243242",
                "parentId": "1450294498671202306",
                "title": "的风格",
                "children": []
              }
            ]
          }
        ]
      }
    ]
  }
}

2、编写SubjectVO

@Data
public class SubjectVO {
    private String id;
    private String parentId;
    private String title;
    private List<SubjectVO> children;
}

3、编写service

@Override
    public List<SubjectVO> listWithTree() {
        List<EduSubject> entities = list();
        //把EduSubject的数组entities包装成SubjectVO数组
        List<SubjectVO> subjectVOS = entities.stream().map(subject -> {
            SubjectVO subjectVO = new SubjectVO();
            BeanUtils.copyProperties(subject, subjectVO);
            return subjectVO;
        }).collect(Collectors.toList());
        //filter是java8的写法,取得所有parentId为0的数据,也就是一级目录
        //map也是java8的写法,用于封装数据,取得他的孩子(也就是下级目录)的数据
        List<SubjectVO> list = subjectVOS.stream().filter(subjectVO ->
                subjectVO.getParentId().equals("0")
        ).map((menu)->{
            menu.setChildren(getChildrenData(menu,subjectVOS));
            return menu;
        }).collect(Collectors.toList());
        return list;
    }
    private List<SubjectVO> getChildrenData(SubjectVO root, List<SubjectVO> all) {
        List<SubjectVO> children = all.stream().filter(subjectVO ->
                subjectVO.getParentId().equals(root.getId())
        ).map(subjectVO -> {
            subjectVO.setChildren(getChildrenData(subjectVO,all));
            return subjectVO;
        }).collect(Collectors.toList());
        return children;
    }

注:本文仅限于个人学习记录
查看原文:https://blog.csdn.net/coding_lin/article/details/113447422

posted @ 2021-10-20 12:01  __先森  阅读(2192)  评论(0编辑  收藏  举报