Skip to content

Commit

Permalink
FEAT: 商品api-品类查询
Browse files Browse the repository at this point in the history
  • Loading branch information
IDen-z committed Nov 9, 2021
1 parent 09fd55c commit 3d45d61
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zmz.shop.product.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -30,6 +31,17 @@ public class CategoryController {
@Autowired
private CategoryService categoryService;

/**
* 查询出所有分类以及子分类 以父子结构显示
*/
@RequestMapping("/list/tree")
//@RequiresPermissions("product:category:list")
public R listTree(){
List<CategoryEntity> res= categoryService.queryListTree();
return R.ok().put("data", res);
}


/**
* 列表
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.zmz.shop.product.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

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

import lombok.Data;

/**
Expand Down Expand Up @@ -57,4 +60,12 @@ public class CategoryEntity implements Serializable {
*/
private Integer productCount;

/**
* 子节点分类List
*/
// 该注解表明未和数据库表的字段一一对应
@TableField(exist = false)
private List<CategoryEntity> childrenList;


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.zmz.common.utils.PageUtils;
import com.zmz.shop.product.entity.CategoryEntity;

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

/**
Expand All @@ -16,5 +17,7 @@
public interface CategoryService extends IService<CategoryEntity> {

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

List<CategoryEntity> queryListTree();
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.zmz.shop.product.service.impl;

import org.springframework.stereotype.Service;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
Expand All @@ -26,4 +31,29 @@ public PageUtils queryPage(Map<String, Object> params) {
return new PageUtils(page);
}

@Override
public List<CategoryEntity> queryListTree() {
// 查出所有的分类列表
List<CategoryEntity> entityList = baseMapper.selectList(new QueryWrapper<>());

// 包装分类实例为父子节点结构

// 过滤所有的顶级节点
List<CategoryEntity> collect = entityList.stream().filter(categoryEntity -> {
return categoryEntity.getParentCid() == 0;
}).map(categoryEntity -> {
categoryEntity.setChildrenList(getChildren(categoryEntity,entityList));
return categoryEntity;
}).sorted(Comparator.comparing(CategoryEntity::getSort)).collect(Collectors.toList());

return collect;
}

/**
* 递归查出当前节点下的每个子节点
*/
private List<CategoryEntity> getChildren(CategoryEntity categoryEntity, List<CategoryEntity> entityList) {
return null;
}

}
6 changes: 5 additions & 1 deletion zshop-product/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
server:
port: 10000
port: 9999

spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.80.130:3306/zshop_pms
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

#classpath加不加*的区别在于加*会把 第三方jar包类路径下匹配到的xml文件也进行包扫描
mybatis-plus:
Expand Down

0 comments on commit 3d45d61

Please sign in to comment.