项目开发中,几乎无法避免的会遇到树形结构,今天和大家分享java后端如何处理数据为树形结构。

前端处理树形结构,后端生成树形结构。

Entity实体层:

此处省略好几个实体字段。

    /**
     * 孩子节点集合
     */
    private List<OrganizationVO> child = new ArrayList<OrganizationVO>();

Controller层:

OrganizationVO list = organizationService.selectCategoryAndChildrenById(id);

Service业务层:

/**
     * 递归查询本节点的id和孩子节点的id
     * @param id
     * @return
     */
    OrganizationVO selectCategoryAndChildrenById(Long id);

ServiceImpl业务实现:

/**
     * 递归查询本节点的id和孩子节点的id
     *
     * @param id
     * @return
     */
    @Override
    public OrganizationVO selectCategoryAndChildrenById(Long id) {
        return findChildCatgoryBak(id);
    }
    
    /**
     * 递归算法-算出子节点
     */
    public OrganizationVO findChildCatgoryBak(Long orgId) {
        List<OrganizationVO> childList = new ArrayList<>();
        // 通过id获取所有VO信息
        OrganizationVO organizationVO = iOrganizationService.get(orgId);

        //查找子节点,递归程序必须要有一个出口
        List<OrganizationVO> organizationList = iOrganizationService.getChildren(null, orgId);
        //organizationVO.setChild(organizationList);
        for (OrganizationVO item : organizationList) {
            childList.add(findChildCatgoryBak(item.getId()));
        }
        organizationVO.setChild(childList);
        return organizationVO;
    }

业务之中的判断自己可以加上,比如通过Id获取这条信息是否为null。

测试结果:

{
  "code": 0,
  "message": "获取组织成功",
  "items": {
    "id": 2,
    "createTime": null,
    "modifyTime": "2020-04-21 16:37:35",
    "creatorId": 0,
    "modifierId": 0,
    "parentId": 0,
    "orgName": "电子商品",
    "orgState": "ENABLE",
    "description": "",
    "orgSort": 1,
    "child": [
      {
        "id": 3,
        "createTime": "2020-04-21 16:41:07",
        "modifyTime": "2020-04-22 10:40:52",
        "creatorId": 0,
        "modifierId": 0,
        "parentId": 2,
        "orgName": "电脑",
        "orgState": "ENABLE",
        "description": "",
        "orgSort": 1,
        "child": [
          {
            "id": 21,
            "createTime": "2020-04-23 23:11:45",
            "modifyTime": "2020-04-23 23:11:45",
            "creatorId": null,
            "modifierId": null,
            "parentId": 3,
            "orgName": "笔记本电脑",
            "orgState": "ENABLE",
            "description": "是佛山东莞给",
            "orgSort": null,
            "child": []
          }
        ]
      },
      {
        "id": 13,
        "createTime": "2020-04-22 16:34:08",
        "modifyTime": "2020-04-22 16:34:08",
        "creatorId": null,
        "modifierId": null,
        "parentId": 2,
        "orgName": "新增组织1",
        "orgState": "ENABLE",
        "description": "",
        "orgSort": 4,
        "child": []
      },
      {
        "id": 14,
        "createTime": "2020-04-22 19:07:58",
        "modifyTime": "2020-04-22 19:07:58",
        "creatorId": null,
        "modifierId": null,
        "parentId": 2,
        "orgName": "测试",
        "orgState": "ENABLE",
        "description": "是佛山东莞给",
        "orgSort": null,
        "child": []
      },
      {
        "id": 16,
        "createTime": "2020-04-23 10:13:26",
        "modifyTime": "2020-04-23 10:13:26",
        "creatorId": null,
        "modifierId": null,
        "parentId": 2,
        "orgName": "112",
        "orgState": "ENABLE",
        "description": null,
        "orgSort": null,
        "child": []
      },
      {
        "id": 20,
        "createTime": "2020-04-23 22:33:10",
        "modifyTime": "2020-04-23 22:33:10",
        "creatorId": null,
        "modifierId": null,
        "parentId": 2,
        "orgName": "测试1111111",
        "orgState": "ENABLE",
        "description": null,
        "orgSort": 3,
        "child": []
      }
    ]
  }
}