栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

谷粒商城 Day02 项目搭建与前后联调

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

谷粒商城 Day02 项目搭建与前后联调

Day02 项目搭建与前后联调 二、项目基础概念 1、三级分类

建立好数据库之后,查询出所有分类商品

SELECt b1.id "一级分类Id",b1.`name` "一级分类名",b2.id "二级分类Id",b2.`name` "二级分类名",b3.id "三级分类Id",b3.`name` "三级分类名"
FROM base_category1 b1
LEFT JOIN base_category2 b2 ON b1.id = b2.category1_id
LEFT JOIN base_category3 b3 ON b2.id = b3.category2_id

此时我们用 EXPLAIN 分析一下

EXPLAIN SELECt b1.id "一级分类Id",b1.`name` "一级分类名",b2.id "二级分类Id",b2.`name` "二级分类名",b3.id "三级分类Id",b3.`name` "三级分类名"
FROM base_category1 b1
LEFT JOIN base_category2 b2 ON b1.id = b2.category1_id
LEFT JOIN base_category3 b3 ON b2.id = b3.category2_id

type 为 ALL,rows 太多了 17×113×1099

发现没有用到索引,此时我们需要加索引,那么应该怎么加?给谁加呢?注意:要遵循小表驱动大表的原则

先看下这几张表

base_category1:一级分类表只有一个主键索引

base_category2:二级分类表可以给 category1_id 建索引

ALTER TABLE base_category2 ADD INDEX idx_category1_id(category1_id)

此时效果明显

接着看 base_category3,可以给 category2_id 建索引

ALTER TABLE base_category3 ADD INDEX idx_category2_id(category2_id)

效果更明显了

2、平台属性 三、商品模块(后台) 1、网关创建

https://spring.io/projects/spring-cloud-gateway

新建 api-gateway

(1)pom.xml

    
        org.springframework.cloud
        spring-cloud-starter-gateway
    

    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-sentinel
    

    
    
        org.springframework.boot
        spring-boot-devtools
        true
    

    
    
        org.springframework.cloud
        spring-cloud-starter-zipkin
    

    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery
    

    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-config
    

(2)bootstrap.properties
spring.application.name=api-gateway
server.port=88
# 指定 nacos 地址
spring.cloud.nacos.server-addr=192.168.200.188:8848
(3)ApiGatewayApplication

新建 com.atguigu.gmall.ApiGatewayApplication

@SpringCloudApplication
public class ApiGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class,args);
    }
}

运行启动类

(4)application.yaml

接下来可以参考官网配置 application.yaml:https://spring.io/projects/spring-cloud-gateway#learn

官网的 application.yaml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - cookie=mycookie,mycookievalue

我们根据后台管理系统和官网的例子修改成自己需要的

spring:
  cloud:
    gateway:
      routes:
        - id: admin-product
          uri: lb://service-product
          predicates:
            - Path=/admin/product/**

2、网关全局跨域配置

(1)CORS Configuration

(2)application.yaml
spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[
@RequestMapping("/admin/product")
@RestController
public class ProductAdminController {

    @Autowired
    CategoryService categoryService;

    
    @GetMapping("/getCategory1")
    public Result> getCategory1(){
        List category1s = categoryService.getCategory1();
        return Result.ok(category1s);
    }

    
    @GetMapping("/getCategory2/{category1Id}")
    public Result> getCategory2(@PathVariable("category1Id") Long category1Id){
        if(category1Id > 0){
            List category2s = categoryService.getCategory2(category1Id);
            return Result.ok(category2s);
        }else {
            return Result.build(null, ResultCodeEnum.SECKILL_ILLEGAL);
        }
    }

    
    @GetMapping("/getCategory3/{category2Id}")
    public Result> getCategory3(@PathVariable("category2Id") Long category2Id){
        if(category2Id > 0){
            List category3s = categoryService.getCategory3(category2Id);
            return Result.ok(category3s);
        }else {
            return Result.build(null, ResultCodeEnum.SECKILL_ILLEGAL);
        }
    }
}
② CategoryService

com.atguigu.gmall.product.service.CategoryService

public interface CategoryService{
    List getCategory1();

    List getCategory2(Long category1Id);

    List getCategory3(Long category2Id);
}
③ CategoryServiceImpl

com.atguigu.gmall.product.service.impl.CategoryServiceImpl

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    baseCategory1Mapper baseCategory1Mapper;

    @Autowired
    baseCategory2Mapper baseCategory2Mapper;

    @Autowired
    baseCategory3Mapper baseCategory3Mapper;

    @Override
    public List getCategory1() {
        //查询所有一级菜单
        List list = baseCategory1Mapper.selectList(null);
        return list;
    }

    @Override
    public List getCategory2(Long category1Id) {
        //查询所有二级菜单
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("category1_id",category1Id);

        return baseCategory2Mapper.selectList(wrapper);
    }

    @Override
    public List getCategory3(Long category2Id) {
        //查询所有三级菜单
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("category2_id",category2Id);

        return baseCategory3Mapper.selectList(wrapper);
    }
}
④ baseCategory1Mapper

com.atguigu.gmall.product.mapper.baseCategory1Mapper

public interface baseCategory1Mapper extends baseMapper {

}
⑤ baseCategory2Mapper

com.atguigu.gmall.product.mapper.baseCategory2Mapper

public interface baseCategory2Mapper extends baseMapper {

}
⑥ baseCategory3Mapper

com.atguigu.gmall.product.mapper.baseCategory3Mapper

public interface baseCategory3Mapper extends baseMapper {

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/657483.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号