建立好数据库之后,查询出所有分类商品
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(2)bootstrap.propertiesorg.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
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{ }



