六、三级分类
1.按照父子类的结构获取所有分类
所有的分类都是一样的表,依靠表的级别和父标签来区分子父表。
要想一次性按父子结构打印出所有的分类,那么分类这个类中必须还得有子类,这个类要存在于entity中,但不能存在于数据库。代码如下:
@TableField(exist = false)
private List<CategoryEntity> children;
@Override
public List<CategoryEntity> listWithTree(){
//1、查出所有分类 null表名没有查找的条件 即全部查出
List<CategoryEntity> entities = baseMapper.selectList(null);
//2、组装成父子的树形结构
//2.1 找到所有的一级分类
List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
categoryEntity.getParentCid()==0
).map((menu)->{
menu.setChildren(getChildrens(menu,entities));
return menu;
}).collect(Collectors.toList());
return level1Menus;
}
private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentCid == root.getCatId();
}).map(categoryEntity -> {
categoryEntity.setChildren(getChildrens(categoryEntity,all));
return categoryEntity;
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort())
}).collect(Collectors.toList());
return children;
}
2.跨域问题的解决
跨域:当前网址与请求的网址 协议、域名、端口三者不完全相同
解决思路:
1、使用nginx部署为同一域
2、让服务器告诉预检请求能跨域
在网关中定义配置类
package com.atguigu.gulimall.gateway.config;
@Configuration // gateway
public class GulimallCorsConfiguration {
@Bean // 添加过滤器
public CorsWebFilter corsWebFilter(){
// 基于url跨域,选择reactive包下的
UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
// 跨域配置信息
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 允许跨域的头
corsConfiguration.addAllowedHeader("*");
// 允许跨域的请求方式
corsConfiguration.addAllowedMethod("*");
// 允许跨域的请求来源
corsConfiguration.addAllowedOrigin("*");
// 是否允许携带cookie跨域
corsConfiguration.setAllowCredentials(true);
// 任意url都要进行跨域配置
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsWebFilter(source);
}
}
这里配置跨域后前端访问还是会出错,因为脚手架工程renren-fast里面也对跨域做了一些配置,需要把那里面的配置注释掉,才能成功。
3.过滤器优先级问题
- id: ware_route
uri: lb://gulimall-ware
predicates:
- Path=/api/ware/**
filters:
- RewritePath=/api/(?<segment>.*),/$\{
segment}
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters: # 这段过滤器和验证码有关,api内容缓存了/renren-fast,还得注意/renren-fast也注册到nacos中
- RewritePath=/api/(?<segment>.*),/renren-fast/$\{
segment}
两个过滤器有明显的层次关系。 /api/** 包含了/api/ware/** 所以,要把比较精确的那个路由放到上面。
也就是把 /api/ware/** 放到 /api/** 的上面、
4.删除
@RequestBody: