一. 数据库表
二. 查询树状结构
/**
* 实体对象添加子集属性
*/
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": []
}
]
}
]
}
]