微服务实战——三级分类实现

三级分类

查询

1.递归树形结构数据获取
/**
   * 查出所有分类以及子分类,以树型结构组装起来
   */
@Override
public List<CategoryEntity> listWithTree() {
    // 1.查出所有分类
    List<CategoryEntity> entities = list();

    // 2.组装成父子的树形结构
    List<CategoryEntity> level1Menus = entities.stream()
    // 2.1.找出所有一级分类
    .filter(categoryEntity -> (categoryEntity.getParentCid().equals(0L)))
    // 2.2.递归并赋值子分类
    .map(categoryEntity -> {
        categoryEntity.setChildren(getChildren(categoryEntity, entities));
        return categoryEntity;
    })
    // 2.3.分类排序
    .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
    .collect(Collectors.toList());

    return level1Menus;
}

// 递归查找所有菜单的子菜单
private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all){

    List<CategoryEntity> children = all.stream()
    // 1.过滤,找到子菜单
    .filter(categoryEntity -> ((categoryEntity.getParentCid()).equals(root.getCatId())))
    // 2.递归,找到子菜单的子菜单,后序遍历赋值
    .map(categoryEntity -> {
        categoryEntity.setChildren(getChildren(categoryEntity, all));
        return categoryEntity;
    })
    // 3.排序
    .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
    .collect(Collectors.toList());

    return children;
}
2.跨域问题解决
package com.yxj.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);
    }
}

3.逻辑删除
mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 1
      logic-not-delete-value: 0
/**
 * 是否显示[0-不显示,1显示]
 */
@TableLogic(value = "1", delval = "0")
private Integer showStatus;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值