多级菜单 树结构 排序 前端 后端 java

目录

省流:

正文:

v1.0版

前端传的值:

后端代码:

v2.0版

v3.0版


省流:

前端提交过来整个树即可。

给整个树进行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方法没写,自己测的时候记得加上

}

相关推荐
黄名富1 分钟前
Redis 附加功能(二)— 自动过期、流水线与事务及Lua脚本
java·数据库·redis·lua
love静思冥想3 分钟前
JMeter 使用详解
java·jmeter
言、雲6 分钟前
从tryLock()源码来出发,解析Redisson的重试机制和看门狗机制
java·开发语言·数据库
TT哇12 分钟前
【数据结构练习题】链表与LinkedList
java·数据结构·链表
Yvemil741 分钟前
《开启微服务之旅:Spring Boot 从入门到实践》(三)
java
Anna。。43 分钟前
Java入门2-idea 第五章:IO流(java.io包中)
java·开发语言·intellij-idea
.生产的驴1 小时前
SpringBoot 对接第三方登录 手机号登录 手机号验证 微信小程序登录 结合Redis SaToken
java·spring boot·redis·后端·缓存·微信小程序·maven
爱上语文1 小时前
宠物管理系统:Dao层
java·开发语言·宠物
王ASC2 小时前
SpringMVC的URL组成,以及URI中对/斜杠的处理,解决IllegalStateException: Ambiguous mapping
java·mvc·springboot·web
是小崔啊2 小时前
开源轮子 - Apache Common
java·开源·apache