feat(新增-分类接口管理): 获取分类树形数据

Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
bunny 2024-03-27 13:58:28 +08:00
parent 52004cf2ba
commit fee9dab785
13 changed files with 167 additions and 1 deletions

View File

@ -21,4 +21,11 @@ public class Knife4jConfig {
return new OpenAPI().info(new Info().title("尚品甑选API接口文档")
.version("1.0").description("尚品甑选API接口文档").contact(new Contact().name("bunny")));
}
@Bean
public GroupedOpenApi webApi() { // 创建了一个api接口的分组
return GroupedOpenApi.builder()
.group("web-api") // 分组名称
.pathsToMatch("/api/**").build(); // 接口请求路径规则
}
}

View File

@ -0,0 +1,41 @@
package com.atguigu.utils;
import com.atguigu.spzx.model.entity.product.Category;
import java.util.ArrayList;
import java.util.List;
public class CategoryHelper {
/**
* 构建树型结构
*
* @param categoryList 分类列表
* @return 结构列表
*/
public static List<Category> buildTree(List<Category> categoryList) {
ArrayList<Category> tree = new ArrayList<>();
// 将菜单进行遍历
categoryList.forEach(category -> {
if (category.getParentId() == 0) {
tree.add(getChildren(category, categoryList));
}
});
return tree;
}
public static Category getChildren(Category category, List<Category> categoryList) {
// 遍历所有菜单数据判断id和parentID对应关系
category.setChildren(new ArrayList<>());
categoryList.forEach(menu -> {
if (menu.getChildren() == null) menu.setChildren(new ArrayList<>());
if (category.getId().equals(menu.getParentId())) {
category.getChildren().add(getChildren(menu, categoryList));
}
});
return category;
}
}

View File

@ -0,0 +1,33 @@
package com.atguigu.product.controller;
import com.atguigu.product.service.CategoryService;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.vo.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
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;
@Tag(name = "分类接口管理")
@RestController
@RequestMapping("/api/product/category/")
@Slf4j
@CrossOrigin
public class CategoryController {
@Autowired
private CategoryService categoryService;
@Operation(summary = "获取分类树形数据", description = "获取分类树形数据")
@GetMapping("findCategoryTree")
public Result<List<Category>> findCategoryTree() {
log.info("===>获取分类树形数据");
List<Category> list = categoryService.findCategoryTree();
return Result.success(list);
}
}

View File

@ -8,6 +8,7 @@ import com.atguigu.spzx.model.vo.h5.IndexVo;
import com.atguigu.spzx.model.vo.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
@ -19,6 +20,7 @@ import java.util.List;
@Tag(name = "首页接口管理")
@RestController
@RequestMapping(value = "/api/product/index")
@Slf4j
@CrossOrigin
public class IndexController {
@Autowired
@ -29,6 +31,7 @@ public class IndexController {
@Operation(summary = "获取首页数据", description = "获取首页数据")
@GetMapping
public Result<IndexVo> findData() {
log.info("===>获取首页数据");
// 查询以及分类
List<Category> categoryList = categoryService.selectOneCategory();
// 根据销量排序获取前十条数据

View File

@ -13,4 +13,11 @@ public interface CategoryMapper {
* @return 分类列表
*/
List<Category> findOneCategory();
/**
* 查询所以分类
*
* @return 分类列表
*/
List<Category> findAll();
}

View File

@ -11,4 +11,11 @@ public interface CategoryService {
* @return 分类实体类列表
*/
List<Category> selectOneCategory();
/**
* 查询属性结构
*
* @return 分类实体类列表
*/
List<Category> findCategoryTree();
}

View File

@ -3,6 +3,7 @@ package com.atguigu.product.service.impl;
import com.atguigu.product.mapper.CategoryMapper;
import com.atguigu.product.service.CategoryService;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.utils.CategoryHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -22,4 +23,16 @@ public class CategoryServiceImpl implements CategoryService {
public List<Category> selectOneCategory() {
return categoryMapper.findOneCategory();
}
/**
* 查询属性结构
*
* @return 分类实体类列表
*/
@Override
public List<Category> findCategoryTree() {
// 查询所以分类返回list集合
List<Category> categoryList = categoryMapper.findAll();
return CategoryHelper.buildTree(categoryList);
}
}

View File

@ -6,5 +6,11 @@ bunny:
username: root
password: "02120212"
redis:
host: 106.15.251.123
port: 6379
database: 2
nacos:
server-addr: z-bunny.cn:8848

View File

@ -17,6 +17,12 @@ spring:
username: ${bunny.datasource.username}
password: "${bunny.datasource.password}"
data:
redis:
host: ${bunny.redis.host}
port: ${bunny.redis.port}
database: ${bunny.redis.database}
logging:
level:
com.atguigu.product.mapper: debug

View File

@ -0,0 +1,16 @@
-----------------▄██-█▄---------
-----------------███▄██▄--------
-----------------███████--------
-----------------▀███████-------
-------------------██████▄▄-----
-------------------█████████▄---
-------------------██████▄████--
-------▄███████████████████████-
-----▄███████████████████████▀--
---▄██████████████████████------
---███████████████████████------
---███████████████████████------
-▄▄██████████████████████▀------
-█████████████████▀█████--------
-▀██████████████▀▀-▀█████▄------
-------▀▀▀▀▀▀▀▀▀------▀▀▀▀------

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -16,4 +16,15 @@
and is_deleted = 0
order by order_num
</select>
<!-- 查询所以分类 -->
<select id="findAll" resultType="com.atguigu.spzx.model.entity.product.Category">
select
<include refid="columns"/>
from category
where
status = 1
and is_deleted = 0
order by order_num
</select>
</mapper>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 设置驼峰标识 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 打印SQL语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<plugins>
<!-- 分页插件 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>