• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
cnsdhzzl
博客园    首页    新随笔    联系   管理    订阅  订阅
菜单树,树形菜单,多级菜单,无限级分类表组装,递归菜单,通用菜单工具

基础版

/**
 * @author zl 
 * @ClassName: Area
 * @Description: 菜单工具类
 * @version: V1.0
 */
public class TreeUtil {
    /**
     * @Param nodes :所有的节点列表
     */
    public List data(List<Area> nodes) {
        ArrayList<Area> rootNode = new ArrayList();
        for (Area node : nodes) {
            if (node.getLevel().equals(0)) {
                rootNode.add(node);
            }
        }
        for (Area node : rootNode) {
            List<Area> child = getChild(node.getAreaCode(), nodes);
            node.setChildren(child);
        }
        return rootNode;
    }


    /**
     * @Description //TODO 获取根节点的子节点
     */
    public List<Area> getChild(Integer id, List<Area> allNode) {
        //存放子菜单的集合
        ArrayList<Area> listChild = new ArrayList();
        for (Area node : allNode) {
            if (node.getParentCode().equals(id)) {
                listChild.add(node);
            }
        }
        //递归:
        for (Area node : listChild) {
            node.setChildren(getChild(node.getAreaCode(), allNode));
        }
        if (listChild.size() == 0) {
            return null;
        }
        return listChild;
    }
}

 

通用高级版

 

import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zl
 * @ClassName: Area
 * @Description: 菜单工具类
 * @version: V1.0
 */
public class TreeUtil {
    // 继承一下,并重新方法,返回相应的id,parentId
    public interface TreeBase {
        String getTreeId();

        String getTreeParentId();
    }

    private static final String GET_TREE_ID = "getTreeId";
    private static final String GET_TREE_PARENT_ID = "getTreeParentId";
    private static final String SET_CHILDREN_METHOD = "setChildren";

    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * @Param nodes :所有的节点列表
     * @Param parentMark :父级标识(是啥就传啥)
     */
    public static <T> List<T> data(List<T> allNodes, Object parentMark) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 转换成指定类型
        List<T> o = (List<T>) objectMapper.convertValue(allNodes, allNodes.getClass());
        // 存储根据节点(父节点)
        ArrayList<T> rootNode = new ArrayList();
        // 遍历所有节点,根据标志寻找父节点
        for (T node : o) {
            Method getTreeParentId = node.getClass().getMethod(GET_TREE_PARENT_ID);
            Object treeParentId = getTreeParentId.invoke(node);
            if (parentMark == null) {
                // 父id标识(是否是父,是父则添加)
                if (treeParentId == parentMark) {
                    rootNode.add(node);
                }
            } else {
                // 父id标识(是否是父,是父则添加)
                if (treeParentId.equals(parentMark)) {
                    rootNode.add(node);
                }
            }
        }
        // 遍历所有父节点,调用递归传入所有节点接口寻找对应子节点
        for (T node : rootNode) {
            Method getTreeId = node.getClass().getMethod(GET_TREE_ID);
            List<T> child = getChild(getTreeId.invoke(node), o);
            if (child != null && !child.isEmpty()) {
                node.getClass().getMethod(SET_CHILDREN_METHOD, List.class).invoke(node, child);
            }
        }
        return rootNode;
    }


    /**
     * @Description //TODO 获取根节点的子节点
     */
    private static <T> List<T> getChild(Object id, List<T> allNode) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 转换成指定类型
        List<T> o = (List<T>) objectMapper.convertValue(allNode, allNode.getClass());
        // 存放子节点
        ArrayList<T> listChild = new ArrayList();
        // 遍历所有节点,根据父节点id寻找字节点
        for (T node : o) {
            Method getTreeParentId = node.getClass().getMethod(GET_TREE_PARENT_ID);
            Object treeParentId = getTreeParentId.invoke(node);
            if (treeParentId != null) {
                // 父id标识(是否是父,是父则添加)
                if (treeParentId.equals(id)) {
                    listChild.add(node);
                }
            }
        }
        //递归:
        for (T node : listChild) {
            List<T> child = getChild(node.getClass().getMethod(GET_TREE_ID).invoke(node), o);
            if (child != null && !child.isEmpty()) {
                node.getClass().getMethod(SET_CHILDREN_METHOD, List.class).invoke(node, child);
            }
        }
        if (listChild.size() == 0) {
            return null;
        }
        return listChild;
    }
}

 

 

 

食用方法:

1.首先使菜单类继承TreeBase

2.菜单类中须有属性children

3.重写两个方法,并对应返回你的菜单类中的‘id’和‘parentId’的值

public class SysDepart implements TreeUtil.TreeBase {
    /**
     * 子节点
     */
    @TableField(exist = false)
    private List<T> children;

    @Override
    public String getTreeId() {
        return getId();
    }

    @Override
    public String getTreeParentId() {
        return getParentId();
    }
}

 

posted on 2020-11-27 09:00  cnsdhzzl  阅读(511)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3