6.商品服务-API三级分类(45-58)

本文档详细介绍了商品服务API如何处理三级分类的递归查询、树形结构展示,以及在Spring Boot中配置网关路由、路径重写、跨域策略。同时,涵盖了前端Vue组件的修改,实现分类的增删改查功能,包括逻辑删除和对话框新增、修改效果。
摘要由CSDN通过智能技术生成

45.查询-递归树形结构数据获取

CategoryController

@RestController
@RequestMapping("product/category")
public class CategoryController {
   
    @Autowired
    private CategoryService categoryService;
    /**
     * 查出所有分类以及子分类,以树行结构组装起来
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R list(){
   
        //查询分类表-全部
        List<CategoryEntity> entities = categoryService.listWithTree();

        return R.ok().put("data", entities);
    }
}

CategoryService

package com.atguigu.gulimall.product.service;

import com.atguigu.gulimall.product.vo.Catelog2Vo;
import com.baomidou.mybatisplus.extension.service.IService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.gulimall.product.entity.CategoryEntity;

import java.util.List;
import java.util.Map;

/**
 * 商品三级分类
 * @author niuniu
 */
public interface CategoryService extends IService<CategoryEntity> {
   

    PageUtils queryPage(Map<String, Object> params);

    List<CategoryEntity> listWithTree();

}

CategoryServiceImpl

以下代码解释:
先收集一级分类,发现表中一级分类的parentCid=0
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
   
    @Override
    public List<CategoryEntity> listWithTree() {
   
		//1.查出所有分类
		List<CategoryEntity> entities = baseMapper.selectList(null);
		//2.组装成父子的树形结构
		//2.1找到所有一级分类
		List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity -> 
			 categoryEntity.getParentCid() == 0
		);
	}
}

CategoryEntity

//新增子分类
@TableField(exist=false)
private List<CategoryEntity> children;

CategoryServiceImpl

2.1找到所有的一级分类
@Override
public List<CategoryEntity> listWithTree() {
   
    //1.查出所有分类
    List<CategoryEntity> entities = baseMapper.selectList(null);
    //2.组装成父子的树形结构
    //2.1.找到所有的一级分类
    List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
            categoryEntity.getParentCid() == 0
    ).map((menu)->{
   
        menu.se
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coder-N

踏实 勤奋 努力 拼搏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值