【六袆- Java】从数据库读取菜单,递归生成菜单树;SpringBoot 动态生成菜单树;SpringBoot操作数据库,动态生成菜单左侧栏;

 

1. 第一种菜单树结构


package com.xiaolh.dao.util;


import com.xiaolh.dao.model.MenuTree;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *    TSysPower树工具类
 * </p>
 *
 * @author: xiaolh
 * @email: amixiao@qq.com
 * @createTime: 2021/3/16  09:53
 */
public abstract class TreeUtil {

    private static final Integer TOP_NODE_ID = 0;

    public static <T> MenuTree<T> buildMenuTree(List<MenuTree<T>> nodes) {
        if (nodes == null) {
            return null;
        }
        //得到父菜单
        List<MenuTree<T>> topNodes = new ArrayList<>();
        nodes.forEach(children -> {
            //得到子菜单的父菜单id
            Integer pid = children.getPowerParentId();
            //如果父节点为0,添加topNodes
            if (pid == null || TOP_NODE_ID.equals(pid)) {
                //把子菜单添加到父菜单
                topNodes.add(children);
       
posted @ 2022-04-26 00:53  你好,Alf  阅读(174)  评论(0编辑  收藏  举报