分类模块
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());
}
}