支付项目:7、分类模块

分类模块

1、因为前端要求的数据里只需要id,parentId, name, sortOrder,所以这里新建一个CategoryVo类,以符合前端的要求。
CategoryVo.java:

package com.xiaoxin.mall.service.vo;

import lombok.Data;
import java.util.List;

@Data
public class CategoryVo {

    private Integer id;

    private Integer parentId;

    private String name;

    private Integer sortOrder;

    private List<CategoryVo> subCategories;
}

2、在CategoryMapper中增加查询所有数据的sql语句:

  <select id="selectAll" resultType="com.xiaoxin.mall.pojo.Category">
    select <include refid="Base_Column_List"/>
    from mall_category
    where status = 1
    order by sort_order DESC
  </select>

3、编写ICategoryServiceImpl.java,注意到,一次性将所有数据读出来后再处理的,因为这样比较快。web项目最耗时的时间在于Http请求和数据库查询操作。

package com.xiaoxin.mall.service.impl;

import com.xiaoxin.mall.consts.MallConst;
import com.xiaoxin.mall.dao.CategoryMapper;
import com.xiaoxin.mall.pojo.Category;
import com.xiaoxin.mall.service.ICategoryService;
import com.xiaoxin.mall.service.vo.CategoryVo;
import com.xiaoxin.mall.service.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ICategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categotyMapper;

    //耗时:http>磁盘>内存
    //mysql(内网+磁盘)
    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
        List<CategoryVo> categoryVoList = new ArrayList<>();
        List<Category> categories = categotyMapper.selectAll();
        for(Category category : categories) {
            if(category.getParentId().equals(MallConst.ROOT_PARENT_ID)) {
                categoryVoList.add(categoryToCategoryVo(category));
            }
        }
        for(CategoryVo categoryVo : categoryVoList) {
            getSubCategories(categoryVo, categories);
        }
        return ResponseVo.success(categoryVoList);
    }

    public void getSubCategories(CategoryVo categoryVo, List<Category> categoryList) {
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for(Category category : categoryList) {
            if(categoryVo.getId().equals(category.getParentId())) {
                categoryVoList.add(categoryToCategoryVo(category));
            }
        }
        categoryVo.setSubCategories(categoryVoList);
        if(categoryVo.getSubCategories().size() > 0) {
            for(CategoryVo categoryVo1 : categoryVo.getSubCategories()) {
                getSubCategories(categoryVo1, categoryList);
            }
        }
    }

    public CategoryVo categoryToCategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);
        return categoryVo;
    }
}

4、编写CategoryController类:

package com.xiaoxin.mall.controller;

import com.xiaoxin.mall.service.ICategoryService;
import com.xiaoxin.mall.service.vo.CategoryVo;
import com.xiaoxin.mall.service.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;


    @GetMapping("/categories")
    public ResponseVo<List<CategoryVo>> categoryInfo() {

        return categoryService.selectAll();
    }
}

5、在拦截器中不要拦截"/categories":

package com.xiaoxin.mall;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login", "/user/register", "/categories");
    }
}

6、单测:

package com.xiaoxin.mall.service.impl;

import com.xiaoxin.mall.MallApplicationTests;
import com.xiaoxin.mall.enums.ResponseEnum;
import com.xiaoxin.mall.service.ICategoryService;
import com.xiaoxin.mall.service.vo.CategoryVo;
import com.xiaoxin.mall.service.vo.ResponseVo;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

import static org.junit.Assert.*;

@Transactional
public class ICategoryServiceImplTest extends MallApplicationTests {

    @Autowired
    private ICategoryService iCategoryService;

    @Test
    public void selectAll() {
        ResponseVo<List<CategoryVo>> listResponseVo = iCategoryService.selectAll();
        Assert.assertEquals(listResponseVo.getStatus(), ResponseEnum.SUCCESS.getCode());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值