zoukankan      html  css  js  c++  java
  • 菜单树,树形菜单,多级菜单,无限级分类表组装,递归菜单,通用菜单工具

    基础版

    /**
     * @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();
        }
    }
  • 相关阅读:
    mysql表结构同步
    关于Java8中lambda约简函数reduce的一个计算问题
    激烈的歌曲有助于编程
    今天刷了数据解构与算法这门课 感觉略有收获
    我有一个好朋友 他的名字叫刘洋 他的ID是北极的大企鹅 他的技术不错 他渴望成为架构师 猎头们路过可以去他的博客看看
    缓存雪崩,缓存击穿,缓存穿透
    celery
    Redis
    django 缓存的使用
    base64 加密
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/14046117.html
Copyright © 2011-2022 走看看