// volist为查询到的数据集合
// 方法1
public List<JSONObject> changeDemandListVoToJSONObjects(List<DemandPlanListVo> voList) {
List<JSONObject> rootList = new ArrayList<JSONObject>()
Map<String, String> parentDMIds = new HashMap<String, String>()
Map<String, String> dmIdMaps = new HashMap<String, String>()
if (!CommonUtil.isEmpty(voList)) {
for (DemandPlanListVo c : voList) {
dmIdMaps.put(c.getBasicId(), c.getBasicId())
String parentDMId = c.getParentId()
if (!StringUtils.isEmpty(parentDMId)) {
parentDMIds.put(parentDMId, parentDMId)
}
}
}
for (DemandPlanListVo c : voList) {
// 若其无父计划或者其父计划不在结果集中,则将其设为root节点
if (StringUtils.isEmpty(c.getParentId()) || StringUtils.isEmpty(dmIdMaps.get(c.getParentId()))) {
JSONObject root = new JSONObject()
root.put("id", c.getId())
root.put("createFullName", c.getCreateFullName())
Date updateTime = c.getUpdateTime()
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String dateString = formatter.format(updateTime)
root.put("updateTime", dateString)
root.put("parentId", c.getParentId())
root.put("parentDemandName()", c.getParentDemandName())
root.put("num", c.getNum())
root.put("demandName", c.getDemandName())
root.put("demandCategoryName", c.getDemandCategoryName())
root.put("demandLevel", c.getDemandLevel())
root.put("demandTypeName", c.getDemandTypeName())
root.put("demandProduct", c.getDemandProduct())
root.put("remark", c.getRemark())
root.put("demandStatus", c.getDemandStatus())
root.put("resourceName", c.getResourceName())
root.put("basicId", c.getBasicId())
root.put("isIcon", c.getIsIcon())
List<JSONObject> rows = new ArrayList<JSONObject>()
root.put("rows", rows)
rootList.add(root)
}
}
for (int i = 0
this.findSubNodeByPid(parentDMIds, voList, rootList.get(i))
}
return rootList
}
`
```
@SuppressWarnings("unchecked")
public void findSubNodeByPid(Map<String, String> parentDMIds, List<DemandPlanListVo> list, JSONObject
parentObject) {
try {
String parentId = parentObject.getString("basicId")
List<JSONObject> subNodeList = new ArrayList<JSONObject>()
for (DemandPlanListVo c : list) {
if (parentId.equals(c.getParentId())) {
JSONObject newNode = new JSONObject()
newNode.put("id", c.getId())
newNode.put("parentId", c.getParentId())
newNode.put("createFullName", c.getCreateFullName())
Date updateTime = c.getUpdateTime()
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String dateString = formatter.format(updateTime)
newNode.put("updateTime", dateString)
newNode.put("parentDemandName", c.getParentDemandName())
newNode.put("num", c.getNum())
newNode.put("demandName", c.getDemandName())
newNode.put("demandCategoryName", c.getDemandCategoryName())
newNode.put("demandLevel", c.getDemandLevel())
newNode.put("demandTypeName", c.getDemandTypeName())
newNode.put("demandProduct", c.getDemandProduct())
newNode.put("remark", c.getRemark())
newNode.put("basicId", c.getBasicId())
newNode.put("demandStatus", c.getDemandStatus())
newNode.put("resourceName", c.getResourceName())
newNode.put("isIcon", c.getIsIcon())
List<JSONObject> rows = new ArrayList<JSONObject>()
newNode.put("rows", rows)
subNodeList.add(newNode)
}
}
if (subNodeList.size() > 0) {
for (int i = 0
List<JSONObject> rows = (List<JSONObject>) parentObject.get("rows")
this.findSubNodeByPid(parentDMIds, list, subNodeList.get(i))
JSONObject currentNode = subNodeList.get(i)
rows.add(currentNode)
parentObject.put("rows", rows)
}
} else {
return
}
} catch (Exception e) {
e.printStackTrace()
}
}
```
// Controller层
```
@GetMapping(value = "/planRelationList")
@ApiOperation("计划需求关系列表")
@AdviceLog(value = "计划需求关系列表", type = LogType.OPERATE, operateType = LogOperateType.QUERY)
public JsonResult<?> planRelationList(DemandPlanListVo vo, HttpServletResponse response) {
try {
List<DemandPlanListVo> list = demandPlanListService.planRelationList(vo)
if (!CommonUtil.isEmpty(list)) {
List<JSONObject> rootList = demandPlanListService.changeDemandListVoToJSONObjects(list)
String resultJSON = JSON.toJSONString(rootList)
try {
response.setCharacterEncoding("utf-8")
response.setContentType("text/html
PrintWriter out = response.getWriter()
out.print(resultJSON)
out.flush()
out.close()
return JsonResult.success(resultJSON)
} catch (IOException e) {
e.printStackTrace()
return JsonResult.error()
}
} else {
String resultJSON = ""
return JsonResult.success(resultJSON)
}
} catch (Exception e) {
e.printStackTrace()
return JsonResult.error()
}
}
```
----------------------------------------方法2-----------------------------------------------
```
// List<Map<String, Object>> list = (List<Map<String, Object>>) sessionFacade.findForJdbc(sql.toString())
public List<Map<String, Object>> getResultList(List<Map<String, Object>> list) {
List<Map<String, Object>> newList = new ArrayList<>()
for (int i = 0
Map<String, Object> map = list.get(i)
String id = (String) map.get("ID")
String parentId = (String) map.get("PARENTID")
// 如果该数据没有父id,则直接查找其子数据
if (CommonUtil.isEmpty(parentId)) {
Map<String, Object> resultMap = getSonList(map, list)
newList.add(resultMap)
}
}
return newList
}
public Map<String, Object> getSonList(Map<String, Object> map, List<Map<String, Object>> list) {
String id = (String) map.get("ID")
List<Map<String, Object>> sonList = new ArrayList()
for (int i = 0
Map<String, Object> map1 = list.get(i)
String parentId = (String) map1.get("PARENTID")
if (id.equals(parentId)) {
Map<String, Object> sonMap = getSonList(map1, list)
sonList.add(sonMap)
}
}
map.put("sonList", sonList)
return map
}
```
------------------------------------------方法3----------------------------------------
// 利用map改进的方法
public Object loadData() {
AjaxResult ajaxResult=new AjaxResult()
//获取权限树
try {
List<Permission> permissionTree=new ArrayList<Permission>()
//所有权限
List<Permission> allPermission = permissionService.queryAllPermission()
Map<Integer,Permission> map=new HashMap<Integer,Permission>()
//100次
for (Permission permission : allPermission) {
map.put(permission.getId(), permission)
}
//100次
for (Permission permission : allPermission) {
Permission child=permission
//找到根权限
if(child.getPid()==null) {
//设置根权限默认打开
child.setOpen(true)
permissionTree.add(child)
}else {
//如果不是根权限,找到他的父权限将其放入父权限的chridren中
Permission parent = map.get(child.getPid())
parent.getChildren().add(child)
}
}
ajaxResult.setData(permissionTree)
ajaxResult.setSuccess(true)
ajaxResult.setMessage("权限树获得成功")
} catch (Exception e) {
e.printStackTrace()
ajaxResult.setSuccess(false)
ajaxResult.setMessage("加载权限树失败")
}
return ajaxResult
}
```
```