• 【微服务】Day08


    开发分类功能

    数据导入

    在给大家提供的csmall-jsd2203项目的doc文件夹下的sql文件夹中

    有多个sql语句文件

    分别去运行它们,我们可以获得酷鲨商城前台的数据库信息了

    我们每个微服务项目原则上只操作少于一个数据库

    随堂更新git地址:

    https://gitee.com/jtzhanghl/csmall-repo-class.git

    分类功能实现逻辑

    我们数据库mall_pms的category表使用自关联实现了三级分类

    当前酷鲨商城项目使用固定的三级分类

    1.从数据库中查询出所有分类信息,一次性全查

    2.构建分类信息的父子结构,实现查询返回父子结构的分类信息

    3.将查询到的结果保存在Redis中,以备后续用户直接获取

    代码中要判断Redis中是否包含全部分类数据,不包含的话做上面操作

    包含分类数据的话直接获得之后返回

    业务分析

    查询全部分类的业务重点在构建三级分类树结构

    我们需要将从数据库中查询出的分类对象构成下面的结构

    [
        {
       id:1,name:"手机/运行商/数码",parentId:0,depth:1,children:[
            {
       id:2,name:"手机通讯",parentId:1,depth:2,children:[
                {
       id:3,name:"智能手机",parentId:2,depth:3,children:null},
                {
       id:4,name:"非智能手机",parentId:2,depth:3,children:null}
            ]}
        ]},
        {
       id:5,name:"电脑/办公",parentId:0,depth:1,children:[....]}
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    上面是我们需要获得的对象的结构

    可以理解为下图

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TjPXDKfn-1664557183696)(image-20220511113842604.png)]

    在数据库mall_pms中

    有pms_category表,这个表就是保存全部分类信息的表格

    id:主键

    name:显示在页面上的分类名称

    parentId:父分类的id 如果是一级分类父分类id为0

    depth:分类深度,当前项目就是3级分类,1\2\3 分别代表它的等级

    keyword:搜索关键字

    sort:排序依据 正常查询时,根据此列进行排序,数字越小越出现在前面(升序)

    icon:图标地址

    enable:是否可用

    isparent:是否为父分类 0 假 1真

    isdisplay:是否显示在导航栏 0不显示 1显示

    实施开发

    在csmall-front-webapi项目中开发

    创建service.impl包

    包中编写业务逻辑层实现类 实现IFrontCategoryService

    @DubboService
    @Service
    @Slf4j
    public class FrontCategoryServiceImpl implements IFrontCategoryService {
       
    
        // 装配操作Redis的对象
        @Autowired
        private RedisTemplate redisTemplate;
        // 当前front模块没有连接数据库的操作,所有数据均来自于Dubbo调用product模块
        // 这里是消费product模块查询所有分类数据的功能
        @DubboReference
        private IForFrontCategoryService dubboCategoryService;
    
        // 开发过程中使用Redis的规范:为了降低Redis使用Key拼写错误的情况,我们会定义常量
        public static final String CATEGORY_TREE_KEY="category_tree";
    
        @Override
        public FrontCategoryTreeVO categoryTree() {
       
            // 我们先检查Redis中是否已经保存了包含所有分类的三级分类树对象
            if(redisTemplate.hasKey(CATEGORY_TREE_KEY)){
       
                // redis中已经包含了三级分类树对象,获取后直接返回即可
                FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
                        (FrontCategoryTreeVO<FrontCategoryEntity>)
                        redisTemplate.boundValueOps(CATEGORY_TREE_KEY).get();
                // 将从redis中获得的treeVO返回
                return treeVO;
            }
            // Redis中没有三级分类树信息,表示本次请求可能是首次访问
            // dubbo调用查询所有分类对象的方法
            List<CategoryStandardVO> categoryStandardVOs=
                            dubboCategoryService.getCategoryList();
            // 我们需要将没有关联子分类能力的CategoryStandardVO类型
            // 转换为具备关联子分类能力的FrontCategoryEntity类型
            // 并将正确的父子分类关系保存构建起来,最好编写一个单独的方法
            FrontCategoryTreeVO<FrontCategoryEntity> treeVO=
                                initTree(categoryStandardVOs);
            // 上面已经完成了三级分类树的构建,下面要将返回值treeVO
            // 保存在redis中,方便后面请求的访问
            redisTemplate
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    上传航测影像就能土方计算?!0基础倾斜摄影土方计算流程
    变电站综合自动化系统的组网与应用
    spring cloud新版本使用loadbalancer替代Ribbon
    运行时系统
    Elasticsearch 7和Elastic Stack:深入实践
    C陷阱与缺陷 第8章 建议与答案 8.2 答案
    day31多线程01
    k8s怎么配置secret呢?
    Leetcode 15. 三数之和
    SOHO帮客户找新品,如何拿到最优价?要不要选择大型机械类产品?
  • 原文地址:https://blog.csdn.net/shortcutsuccess/article/details/127130992