尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

P45 创建三级类目树形数据结构

P45 创建三级类目树形数据结构
📅 发布时间:2026/7/3 3:10:04

1.三级类目实体类创建

catagory表的表结构,首先一级类目的parent_cid为0,二级类目以及三级类目的parent_cid为上一级的cat_id,sort字段用于排序.

与catagory表对应的实体类:

package com.atguigu.gulimail.product.entity; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; import java.util.Date; import java.util.List; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; /** * 商品三级分类 * * @author bryan * @email bryanliu@gulimall.com * @date 2022-09-24 17:49:33 */ @Data @TableName("pms_category") public class CategoryEntity implements Serializable { private static final long serialVersionUID = 1L; /** * 分类id */ @TableId private Long catId; /** * 分类名称 */ private String name; /** * 父分类id */ private Long parentCid; /** * 层级 */ private Integer catLevel; /** * 是否显示[0-不显示,1显示] */ @TableLogic(value = "1", delval = "0") private Integer showStatus; /** * 排序 */ private Integer sort; /** * 图标地址 */ private String icon; /** * 计量单位 */ private String productUnit; /** * 商品数量 */ private Integer productCount; @JsonInclude(JsonInclude.Include.NON_EMPTY) @TableField(exist = false) private List<CategoryEntity> children; }

注意:我们要保存每一类对应的子类,可以在 CategoryEntity 中新增一个属性 children 进行存放。

这里@TableField注解用来标识此属性不在数据库表中存在,防止 MyBatisPlus 集成的 mapper 误识。

2.三级类目查询后台代码实现

首先在 CategoryController 中编写处理请求的方法

/** * 查出所有分类以及子分类,以树形结构组装起来 */ @RequestMapping("/list/tree") public R list(){ List<CategoryEntity> entities = categoryService.listWithTree(); return R.ok().put("data", entities); }

categoryService中的listWithTree具体实现

@Override public List<CategoryEntity> listWithTree() { // 1. 查出所有类 List<CategoryEntity> entities = baseMapper.selectList(null); // 2. 组装成父子的树形结构 List<CategoryEntity> level1Menus = entities.stream().filter((categoryEntity) -> { return categoryEntity.getParentCid() == 0; }).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; } // 递归查找所有菜单的子菜单 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; }

相关新闻

  • python: Steady-State Pattern
  • 基础知识-ISO模型常见协议和每一层作用
  • 程序代码行数统计脚本

最新新闻

  • 澄澈十三水_摆牌总犯规的3个理牌思路
  • JMeter接口测试断言全解析:从核心原理到高级实战策略
  • 衡弈GTO_新手如何系统学德州GTO
  • 基于JavaScript的网盘直链解析工具:多平台API集成架构与高性能下载实现
  • 汽车维修厂业绩稳步增长实战总结(十):配件业务管理的价值与提升清单
  • 机器学习工程师能力进阶指南:你的“段位”到哪一级了?

日新闻

  • JMeter接口测试实战:从核心元件到复杂场景构建
  • Java Applet版刽子手游戏源码:含完整项目结构、吊杆绘图与胜负逻辑
  • 使用Apache JMeter对RoadRunner PHP应用进行性能测试与调优指南

周新闻

  • Windows字体自定义终极方案:No!! MeiryoUI完全指南
  • Deepin Boot Maker:告别命令行,3分钟制作Linux启动盘的智能解决方案
  • Plain Craft Launcher 2:重新定义你的Minecraft游戏体验

月新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号