商品分类查询:递归树形结构数据获取

需求:
如图:
商品分类查询:递归树形结构数据获取_第1张图片
商品分类查询:递归树形结构数据获取_第2张图片
商品分类查询:递归树形结构数据获取_第3张图片

需求:三级分类展示,返回一个树形结构list,前端展示菜单

sql脚本:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for pms_category
-- ----------------------------
DROP TABLE IF EXISTS `pms_category`;
CREATE TABLE `pms_category`  (
  `cat_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '分类id',
  `name` char(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '分类名称',
  `parent_cid` bigint(20) NULL DEFAULT NULL COMMENT '父分类id',
  `cat_level` int(11) NULL DEFAULT NULL COMMENT '层级',
  `show_status` tinyint(4) NULL DEFAULT NULL COMMENT '是否显示[0-不显示,1显示]',
  `sort` int(11) NULL DEFAULT NULL COMMENT '排序',
  `icon` char(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '图标地址',
  `product_unit` char(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '计量单位',
  `product_count` int(11) NULL DEFAULT NULL COMMENT '商品数量',
  PRIMARY KEY (`cat_id`) USING BTREE,
  INDEX `parent_cid`(`parent_cid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1437 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商品三级分类' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

配置yml
server:
  port: 1000

spring:
  application:
    name: gulimall-coupon
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/mall_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml


Controller
package com.example.mybatis.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.mybatis.model.CategoryEntity;
import com.example.mybatis.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: test
 * @description:
 * @author: liulq
 * @create: 2021-12-28 14:52
 */
@RestController
@RequestMapping("cat/cat")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/list")
    public R list(){
        List<CategoryEntity> listed = categoryService.listed();
        return R.ok(listed);
    }
}

CategoryServiceImpl类业务实现类
package com.example.mybatis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatis.mapper.CategoryMapper;
import com.example.mybatis.model.CategoryEntity;
import com.example.mybatis.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
*@program: test
*@description:
*@author: liulq
*@create: 2021-12-28 14:53
*/
@Service("categoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMap;

    @Override
    public List<CategoryEntity> listed() {
        //查出所有分类
        List<CategoryEntity> entities = categoryMap.selectList(null);
        //组装成父子的树形结构
        //找到所有的一级分类
        List<CategoryEntity> collect = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((menu)->{
             menu.setChildren(getChilds(menu,entities));
             return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()-(menu2.getSort()==null?0:menu2.getSort()));
        }).collect(Collectors.toList());
        return collect;
    }

    //递归查找所有菜单的子菜单
    public List<CategoryEntity> getChilds(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            //找到子菜单
            categoryEntity.setChildren(getChilds(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;
    }
}

展示的效果

商品分类查询:递归树形结构数据获取_第4张图片

前端的页面展示类似:

商品分类查询:递归树形结构数据获取_第5张图片

功能完成

你可能感兴趣的:(功能,分类,java-ee)