对于分类来说,一般包括一级分类,二级分类,三级分类,
以商城中某商品的三级分类为例:
1、基本步骤:
1. 从数据库中查询出所有分类信息,一次性全查
2.构建分类信息的父子结构,实现查询返回复制结构的分类信息
3.将查询的结果保存到Redis中,以备后续用户直接获取
2、进行业务分析
查询全部分类的业务重点在**构建三级分类树结构**
3、实施开发
例如 :在csmall-front-webapi项目中开发
创建service.impl包
包中编写业务逻辑层实现类 实现IFrontCategoryService
开过程中使用Redis的规范:为了降低Redis的Key拼写错误的风险,我们会定义常量使用
public static final String CATEGORY_TREE_KEY="category_tree";
需要用到Redis ,需要使用自动装备 来装配Redis对象
@Autowired
private RedisTemplate redisTemplate;
当前front模块没有连接数据库的操作,所有数据均来源于Dubbo调用product模块
所有要消费product模块具备对应功能的接口
@DubboReference
private IForFrontCategoryService dubboCategoryService;
我们先检查Redis中是否包含已经查询出的三级分类树,如果Redis存在该数据,我们就可以直接返回数据即可。
如果Redis中没有三级分类树信息,表示当前请求可能是第一次请求
dubbo调用查询当前数据库所有分类对象的方法
注意:这里需要将没有关联关系的分类列表类型转换为具备关联关系的分类树列表。
为了减少当前业务代码的冗余度,需要自定义一个转换三级分类树的方法。
转换三级分类数的方法:
1、首先要确定所有分类对象的父类对象
(1). 需要声明一个Map,这个Map的key是分类对象的父Id,value是这个父分类Id下的所有子分类对 象 一个父分类Id可以包含多个子分类对象,所以value是个list
(2). 遍历当前分类对象categoryStandardVOs
(3). 因为categoryStandardVO是没有children属性的,不能保存分类关系,所以我们先要把它转换为能够保存父子关系的FrontCategoryEntity,
(4).将同名属性赋值到frontCategoryEntity对象中
(5).因为后面会频繁使用父分类id,所以现在取出
(6).向map中新增当前分类对象到对应的map的key中 ,要先判断这个key是否已经存在,
如果已经存在这个key,就只需要将新对象保存在这个key对应的value的list中。
如果key不存在,我们要创建一个List对象,保存当前分类对象后, 最后当做map的value。
使用list集合的add方法将具备保存父子关系的frontCategoryEntity对象存入value中, 使用map的put方法新增元素,父分类id就是map的key,list就是值value。
2、然后将子分类对象关联到父分类对象的children属性中。
(1).我们从一级分类来开始,将所有对应的子分类赋值到当前父分类的children属性中我们项目设计数据库中父分类id为0,是一级分类,所以我们先获得所有一级分类对象。
(2).判断当前一级分类是否为nul,如果为null,直接抛异常,结束方法。
(3).遍历所有一级分类对象
确定了一级分类对象,可以根据当前一级分类的id,获得它包含的二级分类
获得当前分类的所有子分类对象secondLevels
判断secondLevels是否为空
如果当前一级分类没有二级分类对象,用continue跳过本次循环,继续其它一级分类的遍历
然后遍历二级分类集合
二级分类的对象的id是三级分类的父id
根据这个二级父分类id获得所有该分类下的三级分类对象集合
判断三级分类集合是否为空,如果是空,continue跳过,
将三级分类对象集合赋值到二级分类对象的children属性中
将二级分类对象集合赋值到一级分类对象的children属性中
注意:以上循环遍历时嵌套循环
3、到此为止,所有对象的父子分类关系都已经构建完成 ,
(1).最后将包含所有分类对象的一级分类树集合赋值到该对象中
(2).最后将包含所有分类对象的一级分类树集合赋值到该对象中
(3).千万别忘了返回数据!!! treeVO
代码如下:
private FrontCategoryTreeVO<FrontCategoryEntity> initTree(List<CategoryStandardVO> categoryStandardVOs) {
// 第一部分,确定所有分类对象的父分类
Map<Long,List<FrontCategoryEntity>> map=new HashMap<>();
log.info("当前分类对象总数为:{}",categoryStandardVOs.size());
// 遍历categoryStandardVOs
for(CategoryStandardVO categoryStandardVO: categoryStandardVOs){
FrontCategoryEntity frontCategoryEntity=new FrontCategoryEntity();
// 将同名属性赋值到frontCategoryEntity对象中
BeanUtils.copyProperties(categoryStandardVO,frontCategoryEntity);
// 因为后面会频繁使用父分类id,所以现在取出
Long parentId=frontCategoryEntity.getParentId();
// 向map中新增当前分类对象到对应的map的key中
// 要先判断这个key是否已经存在
if(map.containsKey(parentId)){
map.get(parentId).add(frontCategoryEntity);
}else{
List<FrontCategoryEntity> value=new ArrayList<>();
value.add(frontCategoryEntity);
map.put(parentId,value);
}
}
// 第二部分,将子分类对象关联到父分类对象的children属性中
List<FrontCategoryEntity> firstLevels=map.get(0L);
// 判断当前一级分类是否为null
if (firstLevels==null || firstLevels.isEmpty()){
throw new CoolSharkServiceException(
ResponseCode.INTERNAL_SERVER_ERROR,"当前数据没有根分类");
}
//遍历所有一级分类对象
for(FrontCategoryEntity oneLevel:firstLevels){
// 确定了一级分类对象,可以根据当前一级分类的id,获得它包含的二级分类
Long secondLevelParentId=oneLevel.getId();
// 获得当前分类的所有子分类对象
List<FrontCategoryEntity> secondLevels=map.get(secondLevelParentId);
// 判断secondLevels是否为空
if(secondLevels==null || secondLevels.isEmpty()){
log.warn("当前分类没有二级分类内容:{}",secondLevelParentId);
// 如果当前一级分类没有二级分类对象,跳过本次循环,继续其它一级分类的遍历
continue;
}
// 遍历二级分类集合
for(FrontCategoryEntity twoLevel : secondLevels){
// 二级分类的对象的id是三级分类的父id
// ↓↓↓↓↓↓↓
Long thirdLevelParentId=twoLevel.getId();
// 根据这个二级父分类id获得所有该分类下的三级分类对象集合
List<FrontCategoryEntity> thirdLevels=map.get(thirdLevelParentId);
// 判断三级分类集合是否为空
if(thirdLevels==null || thirdLevels.isEmpty()){
log.warn("当前分类没有三级分类内容:{}",thirdLevelParentId);
continue;
}
// 将三级分类对象集合赋值到二级分类对象的children属性中
twoLevel.setChildrens(thirdLevels);
}
// 将二级分类对象集合赋值到一级分类对象的children属性中
oneLevel.setChildrens(secondLevels);
}
// 到此为止,所有对象的父子分类关系都已经构建完成
FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
new FrontCategoryTreeVO<>();
//将包含所有分类对象的一级分类树集合赋值到该对象中
treeVO.setCategories(firstLevels);
return treeVO;
}
完成转换后,我们就获取到了最后要返回的数据,为了下次访问更方便,我们设计将它存入Redis中,
然后return返回数据。
public FrontCategoryTreeVO categoryTree() {
// 检查Redis中是否包含已经查询出的三级分类树
if(redisTemplate.hasKey(CATEGORY_TREE_KEY)){
// redis中已经包含了三级分类树,直接获取后返回即可
FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
(FrontCategoryTreeVO<FrontCategoryEntity>)
redisTemplate.boundValueOps(CATEGORY_TREE_KEY).get();
// 千万别忘了返回!
return treeVO;
}
//Redis中没有三级分类树信息,表示当前请求可能是第一次请求,
//dubbo调用查询pms数据库所有分类对象的方法
List<CategoryStandardVO>categoryStandardVOs=
dubboCategoryService.getCategoryList();
// 需要将没有关联关系的分类列表CategoryStandardVO类型
// 转换为具备关联关系的分类树列表FrontCategoryEntity类型
// 自定义一个转换三级分类树的方法,减少当前业务代码的冗余度
FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
initTree(categoryStandardVOs);
// 上面方法完成了转换,已经获得了最后要返回的treeVO数据
// 但是为了下次访问更方便,我们项目设计将它保存在Redis中
redisTemplate.boundValueOps(CATEGORY_TREE_KEY).set(treeVO,1, TimeUnit.MINUTES);
// 上面定的时间是针对学习使用的,正常开发分类数据一般都24小时以上
// 这里也得返回!!!treeVO
return treeVO;
}
最终代码汇总:
package cn.tedu.mall.front.service.impl;
import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.front.service.IFrontCategoryService;
import cn.tedu.mall.pojo.front.entity.FrontCategoryEntity;
import cn.tedu.mall.pojo.front.vo.FrontCategoryTreeVO;
import cn.tedu.mall.pojo.product.vo.CategoryStandardVO;
import cn.tedu.mall.product.service.front.IForFrontCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@DubboService
@Service
@Slf4j
public class FrontCategoryServiceImpl implements IFrontCategoryService {
// 开过程中使用Redis的规范:为了降低Redis的Key拼写错误的风险,我们会定义常量使用
public static final String CATEGORY_TREE_KEY="category_tree";
// 所有要消费product模块具备对应功能的接口
@DubboReference
private IForFrontCategoryService dubboCategoryService;
// 操作Redis的对象
@Autowired
private RedisTemplate redisTemplate;
@Override
public FrontCategoryTreeVO categoryTree() {
// 先检查Redis中是否包含已经查询出的三级分类树
if(redisTemplate.hasKey(CATEGORY_TREE_KEY)){
// redis中已经包含了三级分类树,直接获取后返回即可
FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
(FrontCategoryTreeVO<FrontCategoryEntity>)
redisTemplate.boundValueOps(CATEGORY_TREE_KEY).get();
// 千万别忘了返回!
return treeVO;
}
// Redis中没有三级分类树信息,表示当前请求可能是第一次请求
// dubbo调用查询pms数据库所有分类对象的方法
List<CategoryStandardVO> categoryStandardVOs=
dubboCategoryService.getCategoryList();
// 需要将没有关联关系的分类列表CategoryStandardVO类型
// 转换为具备关联关系的分类树列表FrontCategoryEntity类型
// 自定义一个转换三级分类树的方法,减少当前业务代码的冗余度
FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
initTree(categoryStandardVOs);
// 上面方法完成了转换,已经获得了最后要返回的treeVO数据
// 但是为了下次访问更方便,我们项目设计将它保存在Redis中
redisTemplate.boundValueOps(CATEGORY_TREE_KEY)
.set(treeVO,1, TimeUnit.MINUTES);
// 上面定的时间是针对学习使用的,正常开发分类数据一般都24小时以上
// 这里也得返回!!!treeVO
return treeVO;
}
private FrontCategoryTreeVO<FrontCategoryEntity> initTree(List<CategoryStandardVO> categoryStandardVOs) {
// 第一部分,确定所有分类对象的父分类
// 声明一个Map,这个Map的key是分类对象的父Id,value是这个父分类Id下的所有子分类对象
// 一个父分类Id可以包含多个子分类对象,所以value是个list
Map<Long,List<FrontCategoryEntity>> map=new HashMap<>();
log.info("当前分类对象总数为:{}",categoryStandardVOs.size());
// 遍历categoryStandardVOs
for(CategoryStandardVO categoryStandardVO: categoryStandardVOs){
// categoryStandardVO是没有children属性的,不能保存分类关系
// 所以我们先要把它转换为能够保存父子关系的FrontCategoryEntity
FrontCategoryEntity frontCategoryEntity=new FrontCategoryEntity();
// 将同名属性赋值到frontCategoryEntity对象中
BeanUtils.copyProperties(categoryStandardVO,frontCategoryEntity);
// 因为后面会频繁使用父分类id,所以现在取出
Long parentId=frontCategoryEntity.getParentId();
// 向map中新增当前分类对象到对应的map的key中
// 要先判断这个key是否已经存在
if(map.containsKey(parentId)){
// 如果已经存在这个key,就只需要将新对象保存在这个key对应的value的list中
map.get(parentId).add(frontCategoryEntity);
}else{
// 如果当前map没有这个key
// 我们要创建一个List对象,保存当前分类对象后,最后当做map的value
List<FrontCategoryEntity> value=new ArrayList<>();
value.add(frontCategoryEntity);
// map中使用put方法新增元素,parentId为key,list为值
map.put(parentId,value);
}
}
// 第二部分,将子分类对象关联到父分类对象的children属性中
// 第一部分中,我们确定了每个父分类下的所有子分类
// 下面我们从一级分类来开始,将所有对应的子分类赋值到当前父分类的children属性中
// 我们项目设计数据库中父分类id为0,是一级分类,所以我们先获得所有一级分类对象
List<FrontCategoryEntity> firstLevels=map.get(0L);
// 判断当前一级分类是否为null
if (firstLevels==null || firstLevels.isEmpty()){
throw new CoolSharkServiceException(
ResponseCode.INTERNAL_SERVER_ERROR,"当前数据没有根分类");
}
//遍历所有一级分类对象
for(FrontCategoryEntity oneLevel:firstLevels){
// 确定了一级分类对象,可以根据当前一级分类的id,获得它包含的二级分类
Long secondLevelParentId=oneLevel.getId();
// 获得当前分类的所有子分类对象
List<FrontCategoryEntity> secondLevels=map.get(secondLevelParentId);
// 判断secondLevels是否为空
if(secondLevels==null || secondLevels.isEmpty()){
log.warn("当前分类没有二级分类内容:{}",secondLevelParentId);
// 如果当前一级分类没有二级分类对象,跳过本次循环,继续其它一级分类的遍历
continue;
}
// 遍历二级分类集合
for(FrontCategoryEntity twoLevel : secondLevels){
// 二级分类的对象的id是三级分类的父id
// ↓↓↓↓↓↓↓
Long thirdLevelParentId=twoLevel.getId();
// 根据这个二级父分类id获得所有该分类下的三级分类对象集合
List<FrontCategoryEntity> thirdLevels=map.get(thirdLevelParentId);
// 判断三级分类集合是否为空
if(thirdLevels==null || thirdLevels.isEmpty()){
log.warn("当前分类没有三级分类内容:{}",thirdLevelParentId);
continue;
}
// 将三级分类对象集合赋值到二级分类对象的children属性中
twoLevel.setChildrens(thirdLevels);
}
// 将二级分类对象集合赋值到一级分类对象的children属性中
oneLevel.setChildrens(secondLevels);
}
// 到此为止,所有对象的父子分类关系都已经构建完成
// 最后要按照方法要求的返回值来返回
FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
new FrontCategoryTreeVO<>();
// 最后将包含所有分类对象的一级分类树集合赋值到该对象中
treeVO.setCategories(firstLevels);
// 千万别忘了返回!!! treeVO
return treeVO;
}
}