谷粒商城-商品服务三级分类功能·-后端代码

谷粒商城-商品服务三级分类功能·-后端代码_第1张图片

递归获取树形结构

CategoryController

  @RequestMapping("/list/tree")
    public R list(){
        List<CategoryEntity> entities =categoryService.listWithTree();
        return R.ok().put("data",entities);
    }

CategoryServiceImpl
第一步: 1.查出所有分类
第二步:2.组装成父子的树形结构

使用Java Streams和递归实现代码,递归体现递归获取每个分类的子分类。Streams将数据视为数据流,可以对数据进行filter, map, sorted, collect等操作
filter:对数据进行过滤可加过滤条件 返回新的stream
map:map对数据进行映射 映射后数量不变 返回新的stream
sorted :对Stream中的元素进行排序 可加排序条件
collect:将结果收集到一个新的容器中

/**
     * 1.查出所有分类
     * 2.组装成父子的树形结构
     * @return
     */
    @Override
    public List<CategoryEntity> listWithTree() {
        //1.查出所有分类
        List<CategoryEntity>entities =baseMapper.selectList(null);
        //2.组装成父子的树形结构
        //1)找到所有一级分类
        //什么是Streams
        List<CategoryEntity> level1Meaus=entities.stream().filter((categoryEntity)->{
            return categoryEntity.getParentCid()==0;
        }).map((meau)-> {
            meau.setChildren(getChildrens(meau,entities));
            return meau;
        }).sorted((meau1,meau2)->{
            //菜单排序
            return (meau1.getSort()==null?0:meau1.getSort())-(meau2.getSort()==null?0:meau2.getSort());
        }).collect(Collectors.toList());
        return level1Meaus;
    }


    //递归获取每一个菜单的子菜单

    /**
     *
     * @param root 当前分类
     * @param all 所有分类
     */
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children =all.stream().filter(categoryEntity->{
            //分类的父id等于当前id
            return categoryEntity.getParentCid()== root.getCatId();
        }).map(categoryEntity->{
            //递归找子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((meau1,meau2)->{
            //菜单排序
            return (meau1.getSort()==null?0:meau1.getSort())-(meau2.getSort()==null?0:meau2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

配置网关

将请求发送至网关 由网关转发给其他服务
前端配置
更改主页请求发送地址

  window.SITE_CONFIG['baseUrl'] = 'http://localhost:88/api';

将管理模块注册到注册中心
管理模块配置

  application:
    name: renren-fast
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

网关配置

//商品
- id: product_route
          uri: lb://guliproduct
          predicates:
            - Path=/api/guliproduct/**
          filters:
            - RewritePath=/api/(?.*),/$\{segment}
//主页
- id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/** //指定路径的请求转给网关
          filters:
            - RewritePath=/api/(?.*),/renren-fast/$\{segment}

解决跨域问题

前端向后端请求数据由于前后端端口号不同存在跨域问题
解决方法:在网关模块中配置跨域

@Configuration
public class GulimallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration =new CorsConfiguration();
        //1.配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}

在前端通过路径请求即可

菜单删除

批量删除

 @Override
    public void removeMeauByIds(List<Long> asList) {
        //TODO 检查当前删除的菜单何处被引用
        //逻辑删除

        baseMapper.deleteBatchIds(asList);
    }

Mybatis-plus 逻辑删除
1,配置全局逻辑删除规则

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 1
      logic-not-delete-value: 0

2.加上逻辑删除注解

	/**
	 * 是否显示[0-不显示,1显示]
	 */
	@TableLogic(value = "1",delval = "0")
	private Integer showStatus;

修改

批量修改
CategoryServiceImpl

 /**
     * 批量修改
     * @param category
     * @return
     */
    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }

你可能感兴趣的:(谷粒商城,开发语言,spring,spring,cloud)