Java获取树状结构的数据结果

一. 数据库表

二. 查询树状结构

/**
 * 实体对象添加子集属性
 */
public class IndicatorInfoDto extends IndicatorInfo {

    List<IndicatorInfoDto> children;

    public List<IndicatorInfoDto> getChildren() {
        return children;
    }

    public void setChildren(List<IndicatorInfoDto> children) {
        this.children = children;
    }

}
@RequestMapping(value = "/test2")
public Object test2() {
    // sql select * from 表名 where pid = #{pid}
    List<IndicatorInfoDto> indicatorInfoDtos = indicatorInfoMapper.selectByPid(0);
    if (indicatorInfoDtos != null && indicatorInfoDtos.size() > 0) {
        indicatorInfoDtos.forEach(this::findAllChild);
    }
    return indicatorInfoDtos;
}

private void findAllChild(IndicatorInfoDto resource) {
    List<IndicatorInfoDto> indicatorInfoDtos = indicatorInfoMapper.selectByPid(resource.getId());
    resource.setChildren(indicatorInfoDtos);
    if (indicatorInfoDtos != null && indicatorInfoDtos.size() > 0) {
        indicatorInfoDtos.forEach(this::findAllChild);
    }
}

三. 响应结果

[
    {
        "id": 1,
        "sort": 1,
        "pid": 0,
        "indicator": "TuDiLiYongXianZhuang",
        "name": "土地利用现状",
        "indicatorLevel": 0,
        "defaultUnit": "hectare",
        "graunlarity": null,
        "updateFrequency": "year",
        "source": "第三次国土调查及年度变更调查",
        "status": 1,
        "children": [
            {
                "id": 2,
                "sort": 1,
                "pid": 1,
                "indicator": "ShiDi",
                "name": "湿地",
                "indicatorLevel": 1,
                "defaultUnit": "hectare",
                "graunlarity": 5,
                "updateFrequency": "year",
                "source": "第三次国土调查及年度变更调查",
                "status": 1,
                "children": [
                    {
                        "id": 3,
                        "sort": 1,
                        "pid": 2,
                        "indicator": "HongShuLinDi",
                        "name": "红树林地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 4,
                        "sort": 2,
                        "pid": 2,
                        "indicator": "SenLinZhaoZe",
                        "name": "森林沼泽",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 5,
                        "sort": 3,
                        "pid": 2,
                        "indicator": "GuanCongZhaoZe",
                        "name": "灌丛沼泽",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 6,
                        "sort": 4,
                        "pid": 2,
                        "indicator": "ZhaoZeCaoDi",
                        "name": "沼泽草地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 7,
                        "sort": 5,
                        "pid": 2,
                        "indicator": "YanTian",
                        "name": "盐田",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 8,
                        "sort": 6,
                        "pid": 2,
                        "indicator": "YanHaiTanTu",
                        "name": "沿海滩涂",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 9,
                        "sort": 7,
                        "pid": 2,
                        "indicator": "NeiLuTanTu",
                        "name": "内陆滩涂",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 10,
                        "sort": 8,
                        "pid": 2,
                        "indicator": "ZhaoZeDi",
                        "name": "沼泽地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    }
                ]
            },
            {
                "id": 11,
                "sort": 2,
                "pid": 1,
                "indicator": "GengDi",
                "name": "耕地",
                "indicatorLevel": 1,
                "defaultUnit": "hectare",
                "graunlarity": 5,
                "updateFrequency": "year",
                "source": "第三次国土调查及年度变更调查",
                "status": 1,
                "children": [
                    {
                        "id": 12,
                        "sort": 1,
                        "pid": 11,
                        "indicator": "ShuiTian",
                        "name": "水田",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 13,
                        "sort": 2,
                        "pid": 11,
                        "indicator": "ShuiJiaoDi",
                        "name": "水浇地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 14,
                        "sort": 3,
                        "pid": 11,
                        "indicator": "HanDi",
                        "name": "旱地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    }
                ]
            },
            {
                "id": 15,
                "sort": 3,
                "pid": 1,
                "indicator": "ZhongZhiYuanYongDi",
                "name": "种植园用地",
                "indicatorLevel": 1,
                "defaultUnit": "hectare",
                "graunlarity": 5,
                "updateFrequency": "year",
                "source": "第三次国土调查及年度变更调查",
                "status": 1,
                "children": [
                    {
                        "id": 16,
                        "sort": 1,
                        "pid": 15,
                        "indicator": "GuoYuan",
                        "name": "果园",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 17,
                        "sort": 2,
                        "pid": 15,
                        "indicator": "ChaYuan",
                        "name": "茶园",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 18,
                        "sort": 3,
                        "pid": 15,
                        "indicator": "XiangJiaoYuan",
                        "name": "橡胶园",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 19,
                        "sort": 4,
                        "pid": 15,
                        "indicator": "QiTaYuanDi",
                        "name": "其他园地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    }
                ]
            },
            {
                "id": 20,
                "sort": 4,
                "pid": 1,
                "indicator": "LinDi",
                "name": "林地",
                "indicatorLevel": 1,
                "defaultUnit": "hectare",
                "graunlarity": 5,
                "updateFrequency": "year",
                "source": "第三次国土调查及年度变更调查",
                "status": 1,
                "children": [
                    {
                        "id": 21,
                        "sort": 1,
                        "pid": 20,
                        "indicator": "QiaoMuLinDi",
                        "name": "乔木林地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 22,
                        "sort": 2,
                        "pid": 20,
                        "indicator": "ZhuLinDi",
                        "name": "竹林地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    },
                    {
                        "id": 23,
                        "sort": 3,
                        "pid": 20,
                        "indicator": "GuanMuLinDi",
                        "name": "灌木林地",
                        "indicatorLevel": 2,
                        "defaultUnit": "hectare",
                        "graunlarity": 5,
                        "updateFrequency": "year",
                        "source": "第三次国土调查及年度变更调查",
                        "status": 1,
                        "children": []
                    }
                ]
            }
        ]
    }
]

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值