商品分类&轮播广告
因最近又被困在了OSGI技术POC,更新进度有点慢,希望大家不要怪罪哦。
上节 我们实现了登录之后前端的展示,如:
接着,我们来实现左侧分类栏目的功能。
## 商品分类 | ProductCategory |
---|
从上图我们可以看出,商品的分类其实是有层级关系的,而且这种关系一般都是无限层级。在我们的实现中,为了效果的展示,我们仅仅是展示3级分类,在大多数的中小型电商系统中,三级分类完全足够应对SKU的分类。
需求分析
---
先来分析分类都包含哪些元素,以jd
为例:
- logo(logo) 有的分类文字前面会有小标
- 分类展示主图(img_url)
- 主标题(title)
- 副标题/Slogan
- 图片跳转地址(imglinkurl)-- 大多数时候我们点击分类都会
分类Id
跳转到固定的分类商品列表展示页面,但是在一些特殊的场景,比如我们要做一个活动,希望可以点击某一个分类的主图直接定位到活动页面,这个url就可以使用了。 - 上级分类(parent_id)
- 背景色(bg_color)
- 顺序(sort)
- 当前分类级别(type)
开发梳理
---
在上一小节,我们简单分析了一下要实现商品分类的一些points
,那么我们最好在每次拿到需求【开发之前】,对需求进行拆解,然后分解开发流程,这样可以保证我们更好的理解需求,以及在开发之前发现一部分不合理的需求,并且如果需求设计不合理的话,开发人员完全有权,也有责任告知PM。大家的终极目的都是为了我们做的产品更加合理,好用,受欢迎!
- 首次展示,仅仅读取一级分类(Root)
- 根据一级分类查询二三级子分类
编码实现
---
查询一级分类
Service实现
1.在com.liferunner.service
中创建service 接口ICategoryService.java
, 编写查询所有一级分类的方法getAllRootCategorys
,如下:
package com.liferunner.service;
import com.liferunner.dto.CategoryResponseDTO;
import com.liferunner.dto.SecondSubCategoryResponseDTO;
import java.util.List;
/**
* ICategoryService for : 分类service
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
* @since 2019/11/13
*/
public interface ICategoryService {
/**
* 获取所有有效的一级分类(根节点)
*
* @return
*/
List<CategoryResponseDTO> getAllRootCategorys();
}
2.编写实现类com.liferunner.service.ICategoryService.java
@Service
@Slf4j
public class CategorySericeImpl implements ICategoryService {
@Autowired
private CategoryMapper categoryMapper;
@Override
public List<CategoryResponseDTO> getAllRootCategorys() {
Example example = new Example(Category.class);
val conditions = example.createCriteria();
conditions.andEqualTo("type", CategoryTypeEnum.ROOT.type);
val categoryList = this.categoryMapper.selectByExample(example);
//声明返回对象
List<CategoryResponseDTO> categoryResponseDTOS = new ArrayList<>();
if (!CollectionUtils.isEmpty(categoryList)) {
//赋值
CategoryResponseDTO dto;
for (Category category : categoryList) {
dto = new CategoryResponseDTO();
BeanUtils.copyProperties(category, dto);
categoryResponseDTOS.add(dto);
}
}
return categoryResponseDTOS;
}
}
上述代码很好理解,创建tk.mybatis.mapper.entity.Example
,将条件传入,然后使用通用Mapper
查询到type=1
的一级分类,接着将查到的对象列表转换为DTO对象列表。
Controller实现
一般情况下,此类查询都会出现在网站的首页,因此我们来创建一个com.liferunner.api.controller.IndexController
,并对外暴露一个查询一级分类的接口:
package com.liferunner.api.controller;
import com.liferunner.service.ICategoryService;
import com.liferunner.service.IProductService;
import com.liferunner.service.ISlideAdService;
import com.liferunner.utils.JsonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* IndexController for : 首页controller
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
* @since 2019/11/12
*/
@RestController
@RequestMapping("/index")
@Api(value = "首页信息controller", tags = "首页信息接口API")
@Slf4j
public class IndexController {
@Autowired
privat