树形展示三级分类并分页:递归树形结构

今天在写一个项目的时候遇到了一个问题:如何写三级分类的接口?

经过多方查询资料 我解决了该问题

前端页面如下:

 数据库:

 在实体类中添加children

package com.example.humanresources.entity;

import java.util.Date;
import java.io.Serializable;
import java.util.List;

import lombok.Data;
@Data
public class BizProductCategory implements Serializable {
    private static final long serialVersionUID = 892831452437776529L;
    /**
    * 类别id
    */
    
    private Long id;
    /**
    * 类别名称
    */
    
    private String name;
    /**
    * 备注
    */
    
    private String remark;
    /**
    * 排序
    */
    
    private Integer sort;
    
    private Date createTime;
    
    private Date modifiedTime;
    /**
    * 父级分类id
    */
    
    private Long pid;

//    @TableField
    private List<BizProductCategory> children;


}

在mybatis-plus中需要用到@TableField注解

由于我直接用的mybatis 所以直接将children添加为属性即可

因为只对一级分类进行分页,所以我又写了一个实体类:

package com.example.humanresources.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BizProductCategoryPlus {
    private BizProductCategory bizProductCategory;
    private List<BizProductCategory> rows;
    private int total;
}

service层代码:

  @Override
    public BizProductCategoryPlus listWithTree(int  pageNum, int pageSize) {
        // 1.查出所有分类
        List<BizProductCategory> bizProductCategories = bizProductCategoryDao.selectAll();
        // 2.组装成父子树形结构
        // 2.1找出所有的一级分类
        List<BizProductCategory> level1Menus = bizProductCategories.stream().filter(bizProductCategory ->
             bizProductCategory.getPid() == 0
        ).map((menu)->{
            menu.setChildren(getChildrens(menu,bizProductCategories));
            return menu;
        }).sorted((menu1,menu2)->{
            // 菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        //        List<BizProductCategory> level1Menus = bizProductCategories.stream().filter((bizProductCategory) -> {
        //            return bizProductCategory.getPid() == 0;
        //        }).collect(Collectors.toList());
        List<BizProductCategory> page = new ArrayList<>();
        int start = (pageNum-1)*pageSize;
        for (int i = start; i < (start+pageSize > level1Menus.size() ? level1Menus.size() : start+pageSize); i++) {
            page.add(level1Menus.get(i));
        }
        BizProductCategoryPlus bizProductCategoryPlus=new BizProductCategoryPlus();
        bizProductCategoryPlus.setRows(page);
        bizProductCategoryPlus.setTotal(level1Menus.size());
        return bizProductCategoryPlus;
    }

上面调用的递归方法:

//    递归查找所有菜单的子菜单
//    root为当前菜单 all是所有菜单
    private List<BizProductCategory> getChildrens(BizProductCategory root, List<BizProductCategory> all){
        List<BizProductCategory> children = all.stream().filter(bizProductCategory -> {
            return bizProductCategory.getPid() == root.getId();
        }).map(bizProductCategory->{
//   找到子菜单
            bizProductCategory.setChildren(getChildrens(bizProductCategory,all));
            return bizProductCategory;
        }).sorted((menu1,menu2)->{
//    菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

controller层:

/**
     * 分类树形结构
     * @param pageNum
     * @param pageSize
     * @return
     * @author lt
     */
    @GetMapping("categoryTree")
    public R findAll(int  pageNum,int pageSize){
        BizProductCategoryPlus bizProductCategoryPlus = bizProductCategoryService.listWithTree(pageNum, pageSize);
//        BizProductCategoryPlus bizProductCategoryPlus=new BizProductCategoryPlus();
        return R.ok().setData(bizProductCategoryPlus);
}

这样就实现了对一级分类的分页查询

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用 Java 语言编写三级分类递归树形查询的示例代码: ```java import java.util.ArrayList; import java.util.List; public class Category { private int id; private String name; private int parentId; private List<Category> children; public Category(int id, String name, int parentId) { this.id = id; this.name = name; this.parentId = parentId; this.children = new ArrayList<>(); } public void addChild(Category child) { children.add(child); } public int getId() { return id; } public String getName() { return name; } public int getParentId() { return parentId; } public List<Category> getChildren() { return children; } } public class CategoryTree { private List<Category> categories; public CategoryTree(List<Category> categories) { this.categories = categories; } public void buildTree() { for (Category category : categories) { if (category.getParentId() != 0) { Category parentCategory = findParentCategory(category.getParentId()); parentCategory.addChild(category); } } } private Category findParentCategory(int parentId) { for (Category category : categories) { if (category.getId() == parentId) { return category; } } return null; } public void printTree() { for (Category category : categories) { if (category.getParentId() == 0) { printCategory(category, 0); } } } private void printCategory(Category category, int level) { for (int i = 0; i < level; i++) { System.out.print("-"); } System.out.println(category.getName()); for (Category child : category.getChildren()) { printCategory(child, level + 1); } } } public class Main { public static void main(String[] args) { List<Category> categories = new ArrayList<>(); categories.add(new Category(1, "电子产品", 0)); categories.add(new Category(2, "手机", 1)); categories.add(new Category(3, "平板电脑", 1)); categories.add(new Category(4, "笔记本电脑", 1)); categories.add(new Category(5, "服装鞋帽", 0)); categories.add(new Category(6, "男装", 5)); categories.add(new Category(7, "女装", 5)); categories.add(new Category(8, "童装", 5)); categories.add(new Category(9, "玩具", 0)); categories.add(new Category(10, "益智玩具", 9)); categories.add(new Category(11, "模型玩具", 9)); categories.add(new Category(12, "塑料积木", 10)); categories.add(new Category(13, "木质积木", 10)); categories.add(new Category(14, "遥控玩具", 11)); CategoryTree categoryTree = new CategoryTree(categories); categoryTree.buildTree(); categoryTree.printTree(); } } ``` 在这个示例中,我们创建了一个 Category 类来表示分类,每个分类有一个 id、一个名称、一个父分类的 id,以及一个子分类的列表。我们还创建了一个 CategoryTree 类来表示分类树,包含一个分类列表,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值