菜单940217

该博客主要介绍了如何构建和操作菜单数据结构。内容包括DTO类的定义,包含菜单ID、名称、链接、优先级、描述、图标、子菜单集合和父菜单ID等属性。在服务层,实现了查询所有菜单并进行子菜单封装的方法,通过递归构建菜单树,并限制二级菜单最多显示6个。此外,还展示了如何使用Java Stream API对菜单进行分组,以及根据菜单类型(如公告、社区、热点)进行分类。
摘要由CSDN通过智能技术生成
在这里插入代码片
DTO类
    private Integer id;
    private String name; // 菜单名称
    private String linkUrl; // 访问路径
    private Integer priority; // 优先级(用于排序)
    private String description; // 描述
    private String icon;//图标
    private List<Menu> children = new ArrayList<>();//子菜单集合
    private Integer parentMenuId;//父菜单id
    private Integer level;//菜单级别

service层
@Override
 //查询菜单项,并完成子菜单的封装
    public List<Menu> findAllMenu() {
        List<Menu> allMenu  = menuMapper.selectAll();
        List<Menu> menuList = buildByRecursive(allMenu);
        Collections.sort(menuList);
        //2级菜单只需要6个
        for (Menu menu : menuList) {
            List<Menu> children = menu.getChildren();
            while (children.size() > 6) {
                    children.remove(6);
            }

        }
        return menuList;

    /**
     * 使用递归方法建树
     * @param
     * @return
     */
    public  List<Menu> buildByRecursive(List<Menu> menuLists) {
        List<Menu> trees = new ArrayList<>();
        for (Menu menu : menuLists) {
            if ("".equals(menu.getParentMenuId()) || menu.getParentMenuId() == null ) {
                trees.add(findChildren(menu,menuLists));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param
     * @return
     */
    public  Menu findChildren(Menu menu,List<Menu> menuAllLists) {
        for (Menu menuCompared : menuAllLists) {
            if(menu.getId()==menuCompared.getParentMenuId()) {
                if(menu.getChildren() == null) {
                    menu.setChildren(new ArrayList<>());
                }
                //是否还有子节点,如果有的话继续往下遍历,如果没有则直接返回
                menu.getChildren().add(findChildren(menuCompared,menuAllLists));
            }
        }
            Collections.sort(menu.getChildren());
        return menu;
    }

社区文章
流
private Map<String, List<Menu>> groupby(List<Menu> allMenu) {
        Map<String, List<Menu>> collect = allMenu.stream().collect(Collectors.groupingBy(menu -> menu.getMei()));

        return collect;
    }

list<map>
@Override
    public List<Map> findAllMenu2() {
        List<Menu> allMenu  = menuMapper.selectAll();
        List<Map> gb=groupby(allMenu);
        return gb;
    }

    private List<Map> groupby(List<Menu> allMenu) {
        //Map<String, List<Menu>> collect = allMenu.stream().collect(Collectors.groupingBy(menu -> menu.getMei()));
        ArrayList<Map> articles = new ArrayList<>();
        HashMap<String, Object> bulletinmap = new HashMap<>();
        ArrayList<Menu> bulletins = new ArrayList<>();
        ArrayList<Menu> communitys = new ArrayList<>();
        ArrayList<Menu> hots = new ArrayList<>();
        bulletinmap.put("tybe","bulletin");
        HashMap<String, Object> communitymap = new HashMap<>();
        communitymap.put("tybe","community");
        HashMap<String, Object> hotmap = new HashMap<>();
        hotmap.put("tybe","hot");
        for (Menu menu : allMenu) {
            if ("公告".equals(menu.getMei())){
                bulletins.add(menu);
            }
            if ("社区".equals(menu.getMei())){
                communitys.add(menu);
            }if ("热点".equals(menu.getMei())){
                hots.add(menu);
            }
        }
        bulletinmap.put("bulletins",bulletins);
        communitymap.put("communitys",communitys);
        hotmap.put("hots",hots);
        articles.add(bulletinmap);
        articles.add(communitymap);
        articles.add(hotmap);
        return articles;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值