博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个获取多级目录的小案例
阅读量:4146 次
发布时间:2019-05-25

本文共 1809 字,大约阅读时间需要 6 分钟。

文章目录

1、需求

在这里插入图片描述

获取京东商城首页的三级菜单数据

2、代码实现

使用springboot+mybatis实现

1、controller

@RequestMapping("/list/tree")    @ApiOperation(value = "查询三级分类、组装为分类树",notes = "前端通过key==data获取")    public R list(){        List
entities = categoryService.listWithTree(); return R.ok().put("data", entities); }

2、service

/** * 组装分类树 * * @return */@Overridepublic List
listWithTree() { //1、查出所有分类数据 List
entities = baseMapper.selectList(null); //2、组装成父子的树形结构 //2.1、找到所有的一级分类 List
level1Menus = entities.stream() //过滤、拿到所有一级分类 .filter(categoryEntity -> categoryEntity.getParentCid() == 0) //映射、得到所有子分类然后存到children集合内 .map((menu) -> { menu.setChildren(getChildrens(menu, entities)); return menu; }) //排序、进行非空校验并升序排列 .sorted((menu1, menu2) -> { return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort()); }) //收集组装集合 .collect(Collectors.toList()); return level1Menus;}/** * 为所有的父分类找到他的子分类并映射到child集合里、 * * @param root 父分类 * @param all 原始集合数据 * @return */private List
getChildrens(CategoryEntity root, List
all) { List
children = all.stream() //root为父分类、得到所有分类的父Id == root分类Id的数据 .filter(categoryEntity -> { return categoryEntity.getParentCid() == root.getCatId(); }) //映射、然后递归调用、 .map(categoryEntity -> { categoryEntity.setChildren(getChildrens(categoryEntity, all)); return categoryEntity; }) //排序 .sorted((menu1, menu2) -> { return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort()); }) //封装 .collect(Collectors.toList()); return children;}

转载地址:http://yonti.baihongyu.com/

你可能感兴趣的文章
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
一篇搞懂Java反射机制
查看>>
Single Number II --出现一次的数(重)
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
Mysql中下划线问题
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>