美文网首页
商品三级分类-Stream

商品三级分类-Stream

作者: 放开好人 | 来源:发表于2021-03-21 21:21 被阅读0次

一、实现目标

类似商城三级分类展示,一级、二级、三级目录,逐级包含。

二、数据库设计

image.png
  1. 分类id为每个类别进行标号,一级分类可能为1-10。
  2. 父分类id,一级分类默认为0,二级为1-10,三级为二级分类id。
  3. 层级,记录级别,一级就是1,二三同样。当时在想有父分类id是否不需要层级,但随后想到若用父分类id进行层级划分,不管是代码实现还是后期数据查询都会比较麻烦,不如单独列个字段展示来的直接。
  4. 是否显示,非常有必要的字段,生产配置表中有个生效开关简直太棒了。
  5. 排序,直接按照12345排序即可,后台相减比较。商城目录排序是非常有必要的。
  6. 其他都是些公共字段,不做展开介绍。

三、代码实现

3.1 查出该表所有数据及筛选一级菜单

    public List<CategoryEntity> listWithTreeNew(){
        //获取菜单表所有信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //获取所有一级菜单
        List<CategoryEntity> levelOneMenus = categoryEntities.stream().filter((categoryEntity) -> {
            return categoryEntity.getParentCid() == 0;
        }).collect(Collectors.toList());
        return null;
    }
  1. baseMapper为mybatisplus的父类方法的变量,通过泛型继承,父类已经注入,直接使用即可。
  2. java\util\stream\Stream接口详细介绍:https://blog.csdn.net/y_k_y/article/details/84633001
    本人粗略测试filter方法后总结:
    a. Collection集合接口有Stream方法,Map无。list和set都可以使用。将其转化为流。
    b. filter内的方法,参数为list遍历的对象。比如图中参数为每次遍历的CategoryEntity类的对象,括号内只是参数名称,类似于方法传参的别名。
    categoryEntities.stream().filter((CategoryEntity 别名) -> {
    //各种筛选条件,或者适当逻辑亦可
    return true;
    }).collect(Collectors.toList());
    c. lmade表达式中,方法返回值为布尔类型。
    d. 若返回true则说明该数据符合进行保留,false则舍弃。
    e. 若参数一个,圆括号可以省略,方法体只有一行,大括号可以省略。
    f. 当时在想为何拿父分类为0来筛选一级,而不是直接拿层级。后来想一级夫分类修改几率不大,而层级几率大些。
   public List<CategoryEntity> listWithTreeNew(){
        //获取菜单表所有信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //获取所有一级菜单
        List<CategoryEntity> levelOneMenus = categoryEntities.stream().filter(categoryEntity -> 
            categoryEntity.getParentCid() ==0
        ).collect(Collectors.toList());
        return null;
    }

3.2 一级目录加装二级菜单

    public List<CategoryEntity> listWithTree() {
        //获取菜单表所有信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //获取所有一级菜单
        List<CategoryEntity> levelOneMenus = categoryEntities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() ==0
        ).collect(Collectors.toList());
        //map用于映射每个元素到对应的结果
        List<CategoryEntity> collect = levelOneMenus.stream().map((oneMenus) -> {
            //得到一级菜单下所有子菜单
            List<CategoryEntity> collect1 = categoryEntities.stream().filter((categoryEntity) -> {
                return categoryEntity.getParentCid() == oneMenus.getCatId();
            }).collect(Collectors.toList());
            oneMenus.setChildren(collect1);
            return oneMenus;
        }).collect(Collectors.toList());
        return null;

a. map用于映射每个元素到对应的结果。说白了每个遍历做一下功能增强。
b. 上述代码中,可以再次进行嵌套将三级菜单放入二级菜单的setChildren中。但显得代码不简洁。

3.3 递归调用

    public List<CategoryEntity> listWithTree() {
        //获取菜单表所有信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //获取所有一级菜单
        List<CategoryEntity> levelOneMenus = categoryEntities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() ==0
        ).map(oneMenus -> {
            oneMenus.setChildren(getChildrens(oneMenus,categoryEntities));
            return oneMenus;
        }).collect(Collectors.toList());
        return null;
    }
    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).collect(Collectors.toList());
        return children;
    }

a. 此代码则无需考虑菜单级别问题,就算有十级菜单亦可轻松应对。
b. 另外对主方法多个stream进行了合并,效果相同。

3.4 增加排序

    public List<CategoryEntity> listWithTree() {
        //获取菜单表所有信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //获取所有一级菜单
        List<CategoryEntity> levelOneMenus = categoryEntities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() ==0
        ).map(oneMenus -> {
            oneMenus.setChildren(getChildrens(oneMenus,categoryEntities));
            return oneMenus;
        }).sorted((menu1,menu2)->{
            //2、菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return null;
    }
    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            //2、菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

a. sorted提供排序方法,返回值为int。细节不清,后续研究。

相关文章

  • 商品三级分类-Stream

    一、实现目标 类似商城三级分类展示,一级、二级、三级目录,逐级包含。 二、数据库设计 分类id为每个类别进行标号,...

  • 三级分类级联查询

    商品分类下有三级 ​​​ 查询结果: ​​​

  • 使用java爬虫WebCollector+jsoup抓取商品分类

    背景介绍: 场景是,有京东三级分类名称,没有对应图标,需要根据京东三级分类名称,获取分类名称匹配的图片,来作为商品...

  • 商品管理表概述

    这八个表就是一个商品管理,它主要的功能是一级分类,二级分类,和三级分类,这三个分类是一个三级联动实现的,而这个三级...

  • 订单数据分析,并进行了一次sql优化

    一、需求 订单销量数据统计,按商品分类(二级,三级)或商品统计,平均采购单价,毛利单价,毛利金额,毛利率等销量 =...

  • list集合 树 三层集合嵌套 {..{..{..}}}

    手机版商品分类 查询一级分类所有 和一级分类下第一个子元素的二级集合 和第一个分类下的所有三级集合 //记录分类编...

  • Day003 商品应用需求分析 及 Django 开发介绍

    管理员需要做什么? 管理商品分类:新建分类、编辑分类和删除分类。 管理商品:新建商品、编辑商品。 管理商品规格:新...

  • TableView结合CollectionView展示三级列表

    先来看一张效果图 需求分析 做一个商品分类展示,有三级列表,要求第一级列表能展开,显示二三级列表,同时就还能收起。...

  • Flutter中的Stream初探

    Stream: 超级抽象的一个XXX Stream 的分类 (1) "Single-subscription" ...

  • PHP简单电商平台

    搭建框架,添加常用辅助函数 设计数据库 实现后台功能 商品分类 功能 添加商品分类,显示商品分类,编辑商品分类,删...

网友评论

      本文标题:商品三级分类-Stream

      本文链接:https://www.haomeiwen.com/subject/hpfocltx.html