目录
前端提交过来整个树即可。
给整个树进行sort。代码如下:
- public static void sort(List
tree) { - int i = 0;
- for (Node o : tree) {
- o.setSort(i++);
- if(o.getChild()!=null){
- sort(o.getChild());
- }
- }
- }
这个只是单纯排序,没有替换parentId。升级版本见正文。
排序完以后,结果如下:
一级节点的sort:1,2,3,4...
二级节点的sort:每个一级节点下的二级节点,都会从1开始排序:1,2,3,4...
- |-- 一级节点1
- |--二级节点1
- |--二级节点2
- |--二级节点3
- |-- 一级节点2
- |--二级节点1
- |--二级节点2
- |--二级节点3
- |-- 一级节点3
- |--二级节点1
- |--二级节点2
- |--二级节点3
- {
- "id": "雪花id",
- "parentId": "雪花id",
- "name":"书籍",
- "sort":"1",
- "children": [
- {
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "数学",
- "sort": 1,
- "children": [
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "几何学",
- "sort": 1,
-
- },
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "代数学",
- "sort": 2,
- },
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "概率学",
- "sort": 3,
- }
- ]
-
- },
- {
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "物理",
- "sort": 2,
- "children": [
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "光学",
- "sort": 1,
-
- },
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "力学",
- "sort": 2,
- },
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "量子学",
- "sort": 3,
- }
- ]
- },
- {
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "化学",
- "sort": 3,
- "children": [
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "有机学",
- "sort": 1,
-
- },
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "无机学",
- "sort": 2,
- },
- {
- "children": [],
- "id": "雪花id",
- "parentId": "雪花id",
- "name": "应用化学",
- "sort": 3,
- }
- ]
- }
- ]
- }
后端模拟前端传值单元测试:
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
-
- public class SortT1 {
- public static void main(String[] args) {
- //一级节点
- Node firstNode1 = new Node();
- Node firstNode2 = new Node();
- Node firstNode3 = new Node();
- List
first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3)); -
- //二级节点
- Node sec1First1 = new Node();
- Node sec2First1 = new Node();
- Node sec3First1 = new Node();
- List
second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1)); - firstNode1.setChild(second1);
-
- Node sec1First2 = new Node();
- Node sec2First2 = new Node();
- Node sec3First2 = new Node();
- List
second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2)); - firstNode2.setChild(second2);
-
- Node sec1First3 = new Node();
- Node sec2First3 = new Node();
- Node sec3First3 = new Node();
- List
second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3)); - firstNode3.setChild(second3);
-
- sort(first);
-
- System.out.println(first);
- }
-
- public static void sort(List
tree) { - int i = 0;
- for (Node o : tree) {
- o.setSort(i++);
- if(o.getChild()!=null){
- sort(o.getChild());
- }
- }
- }
- }
- import java.util.List;
-
- public class Node {
- private Integer sort;
- private List
child; -
- public Integer getSort() {
- return sort;
- }
-
- public void setSort(Integer sort) {
- this.sort = sort;
- }
-
- public List
getChild() { - return child;
- }
-
- public void setChild(List
child) { - this.child = child;
- }
- }
增加parentId
- public static void main(String[] args) {
- //一级节点
- Node firstNode1 = new Node("0001");
- Node firstNode2 = new Node("0002");
- Node firstNode3 = new Node("0003");
- List
first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3)); -
- //二级节点
- Node sec1First1 = new Node("00010001");
- Node sec2First1 = new Node("00010002");
- Node sec3First1 = new Node("00010003");
- List
second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1)); - firstNode1.setChild(second1);
-
- Node sec1First2 = new Node("00020001");
- Node sec2First2 = new Node("00020002");
- Node sec3First2 = new Node("00020003");
- List
second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2)); - firstNode2.setChild(second2);
-
- Node sec1First3 = new Node("00030001");
- Node sec2First3 = new Node("00030002");
- Node sec3First3 = new Node("00030003");
- List
second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3)); - firstNode3.setChild(second3);
-
- sort(first, "0");
-
- System.out.println(first);
- }
- public static void sort(List
tree,String parentId) { - int i = 0;
- for (Node o : tree) {
- o.setSort(i++);
- o.setParentId(parentId);
- if(o.getChild()!=null){
- sort(o.getChild(),o.getId());
- }
- }
- }
- public class Node {
- public Node(String id){
- this.id = id;
- }
- private Integer sort;
- private List
child; - private String id;
- private String parentId;
- //省略了get set方法没写,自己测的时候记得加上
- }
保存到数据库:将所有节点都放到一个list里。
- public static void main(String[] args) {
- //省略前面的代码
- //...
- List
result = new ArrayList<>(); - sort(first, "0", result);
- //将结果集保存到数据库
- xxxMapper.saveOrUpdate(result);
- }
-
- public static void sort(List
tree,String parentId,List result) { - int i = 0;
- for (Node o : tree) {
- o.setSort(i++);
- o.setParentId(parentId);
- if(o.getChild()!=null){
- sort(o.getChild(),o.getId(),result);
- }
- o.setChild(null);//递归走出来后就不需要child了
- result.add(o);//将当前节点存到结果集里
- }
- }
注意:为了避免意外发生,生产上记得加上深度,以防万一出现死循环导致栈溢出stackoverflow。
===================分割线===================
文章到此已经结束,以下是紫薯布丁
|-- 一级节点1
|--二级节点1
|--二级节点2
|--二级节点3
|-- 一级节点2
|--二级节点1
|--二级节点2
|--二级节点3
|-- 一级节点3
|--二级节点1
|--二级节点2
|--二级节点3
{
"id": "雪花id",
"parentId": "雪花id",
"name":"书籍",
"sort":"1",
"children": [
{
"id": "雪花id",
"parentId": "雪花id",
"name": "数学",
"sort": 1,
"children": [
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "几何学",
"sort": 1,
},
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "代数学",
"sort": 2,
},
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "概率学",
"sort": 3,
}
]
},
{
"id": "雪花id",
"parentId": "雪花id",
"name": "物理",
"sort": 2,
"children": [
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "光学",
"sort": 1,
},
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "力学",
"sort": 2,
},
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "量子学",
"sort": 3,
}
]
},
{
"id": "雪花id",
"parentId": "雪花id",
"name": "化学",
"sort": 3,
"children": [
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "有机学",
"sort": 1,
},
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "无机学",
"sort": 2,
},
{
"children": [],
"id": "雪花id",
"parentId": "雪花id",
"name": "应用化学",
"sort": 3,
}
]
}
]
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SortT1 {
public static void main(String[] args) {
//一级节点
Node firstNode1 = new Node();
Node firstNode2 = new Node();
Node firstNode3 = new Node();
List
//二级节点
Node sec1First1 = new Node();
Node sec2First1 = new Node();
Node sec3First1 = new Node();
List
firstNode1.setChild(second1);
Node sec1First2 = new Node();
Node sec2First2 = new Node();
Node sec3First2 = new Node();
List
firstNode2.setChild(second2);
Node sec1First3 = new Node();
Node sec2First3 = new Node();
Node sec3First3 = new Node();
List
firstNode3.setChild(second3);
sort(first);
System.out.println(first);
}
public static void sort(List
int i = 0;
for (Node o : tree) {
o.setSort(i++);
if(o.getChild()!=null){
sort(o.getChild());
}
}
}
}
public static void main(String[] args) {
List
sort(first, "0", result);
//将结果集保存到数据库
xxxMapper.saveOrUpdate(result);
}
public static void sort(List
int i = 0;
for (Node o : tree) {
o.setSort(i++);
o.setParentId(parentId);
if(o.getChild()!=null){
sort(o.getChild(),o.getId(),result);
}
o.setChild(null);//递归走出来后就不需要child了
result.add(o);//将当前节点存到结果集里
}
}
public class Node {
public Node(String id){
this.id = id;
}
private Integer sort;
private List
private String id;
private String parentId;
//省略了get set方法没写,自己测的时候记得加上
}