目录
省流:
前端提交过来整个树即可。
给整个树进行sort。代码如下:
java
public static void sort(List<Node> 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...
bash
|-- 一级节点1
|--二级节点1
|--二级节点2
|--二级节点3
|-- 一级节点2
|--二级节点1
|--二级节点2
|--二级节点3
|-- 一级节点3
|--二级节点1
|--二级节点2
|--二级节点3
正文:
v1.0版
前端传的值:
bash
{
"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,
}
]
}
]
}
后端代码:
后端模拟前端传值单元测试:
java
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> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));
//二级节点
Node sec1First1 = new Node();
Node sec2First1 = new Node();
Node sec3First1 = new Node();
List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));
firstNode1.setChild(second1);
Node sec1First2 = new Node();
Node sec2First2 = new Node();
Node sec3First2 = new Node();
List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));
firstNode2.setChild(second2);
Node sec1First3 = new Node();
Node sec2First3 = new Node();
Node sec3First3 = new Node();
List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));
firstNode3.setChild(second3);
sort(first);
System.out.println(first);
}
public static void sort(List<Node> tree){
int i = 0;
for (Node o : tree) {
o.setSort(i++);
if(o.getChild()!=null){
sort(o.getChild());
}
}
}
}
java
import java.util.List;
public class Node {
private Integer sort;
private List<Node> child;
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public List<Node> getChild() {
return child;
}
public void setChild(List<Node> child) {
this.child = child;
}
}
v2.0版
增加parentId
java
public static void main(String[] args) {
//一级节点
Node firstNode1 = new Node("0001");
Node firstNode2 = new Node("0002");
Node firstNode3 = new Node("0003");
List<Node> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));
//二级节点
Node sec1First1 = new Node("00010001");
Node sec2First1 = new Node("00010002");
Node sec3First1 = new Node("00010003");
List<Node> 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<Node> 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<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));
firstNode3.setChild(second3);
sort(first, "0");
System.out.println(first);
}
public static void sort(List<Node> 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());
}
}
}
java
public class Node {
public Node(String id){
this.id = id;
}
private Integer sort;
private List<Node> child;
private String id;
private String parentId;
//省略了get set方法没写,自己测的时候记得加上
}
v3.0版
保存到数据库:将所有节点都放到一个list里。
java
public static void main(String[] args) {
//省略前面的代码
//...
List<Node> result = new ArrayList<>();
sort(first, "0", result);
//将结果集保存到数据库
xxxMapper.saveOrUpdate(result);
}
public static void sort(List<Node> tree,String parentId,List<Node> 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> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));
//二级节点
Node sec1First1 = new Node();
Node sec2First1 = new Node();
Node sec3First1 = new Node();
List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));
firstNode1.setChild(second1);
Node sec1First2 = new Node();
Node sec2First2 = new Node();
Node sec3First2 = new Node();
List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));
firstNode2.setChild(second2);
Node sec1First3 = new Node();
Node sec2First3 = new Node();
Node sec3First3 = new Node();
List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));
firstNode3.setChild(second3);
sort(first);
System.out.println(first);
}
public static void sort(List<Node> tree){
int i = 0;
for (Node o : tree) {
o.setSort(i++);
if(o.getChild()!=null){
sort(o.getChild());
}
}
}
}
public static void main(String[] args) {
List<Node> result = new ArrayList<>();
sort(first, "0", result);
//将结果集保存到数据库
xxxMapper.saveOrUpdate(result);
}
public static void sort(List<Node> tree,String parentId,List<Node> 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);//将当前节点存到结果集里
}
}
public class Node {
public Node(String id){
this.id = id;
}
private Integer sort;
private List<Node> child;
private String id;
private String parentId;
//省略了get set方法没写,自己测的时候记得加上
}