1.首先是java代碼實現樹形結構
1.1可以直接上代碼,有一般遞歸和JAVA8 Stream新特性實現
private List<Department> children = new ArrayList<>(); 注意這個要new出來,不能直接寫成 private List<Department> children; 不然JAVA8 Stream 方法會報空指針,可以試試!!!!!!!!!!
package com.example.demo.vo; import java.util.ArrayList; import java.util.List; public class Department { private Integer id; private String name; private Integer parentId; private List<Department> children = new ArrayList<>(); public List<Department> getChildren() { return children; } public void setChildren(List<Department> children) { this.children = children; } public Department(Integer id, String name, Integer parentId) { this.id = id; this.name = name; this.parentId = parentId; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } @Override public String toString() { return "Department{" + "id=" + id + ", name='" + name + '\'' + ", parentId=" + parentId + ", children=" + children + '}'; } }
package com.example.demo.vo; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class DeptTreeUtil { public static List<Department> initData(){ List<Department> departmentList = new ArrayList<>(); departmentList.add(new Department(1, "江蘇省", 0)); departmentList.add(new Department(2, "南京", 1)); departmentList.add(new Department(3, "無錫", 1)); departmentList.add(new Department(4, "湖北省", 0)); departmentList.add(new Department(5, "武漢市", 4)); departmentList.add(new Department(6, "武昌", 5)); departmentList.add(new Department(7, "漢口", 5)); departmentList.add(new Department(8, "漢陽", 5)); departmentList.add(new Department(9, "孝感市", 4)); departmentList.add(new Department(10, "雲夢縣", 9)); departmentList.add(new Department(11, "測試", 100)); return departmentList; } //遞歸方法一 java8新特性 public static List<Department> makeTree(List<Department> departmentList, Integer pId) { //子類 List<Department> children = departmentList.stream().filter(x -> x.getParentId() .equals(pId) ).collect(Collectors.toList()); //后輩中的非子類 List<Department> successor = departmentList.stream().filter(x -> !x.getParentId() .equals(pId) ).collect(Collectors.toList()); children.forEach(x -> { makeTree(successor, x.getId()).forEach( y -> x.getChildren().add(y) ); } ); return children; } //遞歸方法一 普通遞歸 public static void getChildList(List<Department> list,List<Department> rootList){ // 得到子節點列表 for (Department department : rootList){ List<Department> childRenList = new ArrayList<>(); for (Department department1 : list){ if(department.getId().equals(department1.getParentId())){ childRenList.add(department1); } } if(childRenList.size() > 0){ department.setChildren(childRenList); getChildList(list,childRenList); } } } }
controller層主要是便於接口測試看到json格式
package com.example.demo.controller; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.vo.Department; import com.example.demo.vo.DeptTreeUtil; @RestController public class TreeController { @RequestMapping("/getDeptTree") public Object testTree(){ //獲取部門數據 List<Department> list = DeptTreeUtil.initData(); //java8 Stream處理后的tree行結構數據 List<Department> tree = DeptTreeUtil.makeTree(list, 0); return tree; } @RequestMapping("/getDeptTree2") public Object getTree(){ List<Department> list = DeptTreeUtil.initData(); List<Department> rootList = new ArrayList<>(); //先獲取列表中parentId為0的根節點 for (Department department : list){ if(department.getParentId().equals(0)){ rootList.add(department); } } DeptTreeUtil.getChildList(list, rootList); return rootList; } }
測試結果可以用postman可以看到json各式如下,兩個接口都是一樣的結果:
[ { "id": 1, "name": "江蘇省", "parentId": 0, "children": [ { "id": 2, "name": "南京", "parentId": 1, "children": [] }, { "id": 3, "name": "無錫", "parentId": 1, "children": [] } ] }, { "id": 4, "name": "湖北省", "parentId": 0, "children": [ { "id": 5, "name": "武漢市", "parentId": 4, "children": [ { "id": 6, "name": "武昌", "parentId": 5, "children": [] }, { "id": 7, "name": "漢口", "parentId": 5, "children": [] }, { "id": 8, "name": "漢陽", "parentId": 5, "children": [] } ] }, { "id": 9, "name": "孝感市", "parentId": 4, "children": [ { "id": 10, "name": "雲夢縣", "parentId": 9, "children": [] } ] } ] } ]
2.然后在mySql語句其實也可以實現
2.1先創建表如下,當然這是ORACLE的語法,mysql需要些存儲過程,有需要可以自己去學學,目前我還不會,以后補上去
CREATE TABLE TBL_TEST ( ID NUMBER, NAME VARCHAR2(100 BYTE), PID NUMBER DEFAULT 0 ); #插入測試數據: 可以自己進行插入 #從Root往樹末梢遞歸 select * from TBL_TEST start with id=1 connect by prior id = pid #從末梢往樹ROOT遞歸 select * from TBL_TEST start with id=5 connect by prior pid = id