前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >7_首页分类按级别展示[通俗易懂]

7_首页分类按级别展示[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-12 21:08:04
3680
发布2022-09-12 21:08:04
举报

大家好,又见面了,我是你们的朋友全栈君。

需求

在首页展示商品分类列表

属性

请求 url

http://{zuul:port}/api/portal/categorys/categorys

请求方式

GET

参数

返回值

HttpResult.ok(分类列表)


实现

1、pojo 实体类

要展示商品分类信息,需要树形结构展示,所以需要在 Itemcat 类中增加一个 childern 属性表示子分类

在这里插入图片描述
在这里插入图片描述

比如:我们查看一级目录有:家电、手机、服装,但在家电这个一级目录下,又有分类,我们称为二级目录,有:电视、空调、洗衣机,但是电视下又有分类,所以我们需要给分类实体类增加一个属性,用来表示子分类

如果使用通用 mapper,在 pojo 类中增加额外属性的话,需要使用 @Transient 注解标注,否则会报错

代码语言:javascript
复制
/** * transient:表示当前属性是瞬时属性,跟字段没有映射,默认自动映射同名的字段 */
@Transient
private List<Itemcat> children;

2、supergo_manager 微服务

supergo_manager_service8001
Service

tk-mybatis 提供的基础增删改查并不能满足我们的需求,我们还需要自己定义方法

接口

代码语言:javascript
复制
package com.supergo.service;

import com.supergo.pojo.Itemcat;
import com.supergo.service.base.BaseService;

import java.util.List;

/** * @Author: xj0927 * @Description: 分类查询接口 * @Date Created in 2020-12-25 16:16 * @Modified By: */
public interface ItemcatService extends BaseService<Itemcat> { 
   
    List<Itemcat> getItemCatList();
}

实现类

代码语言:javascript
复制
package com.supergo.service.imp;

import com.supergo.pojo.Itemcat;
import com.supergo.service.ItemcatService;
import com.supergo.service.base.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;

/** * @Author: xj0927 * @Description: 使用递归: 解决多级查询的问题 * @Date Created in 2020-12-25 16:16 * @Modified By: */
@Service
public class ItemcatServiceImpl extends BaseServiceImpl<Itemcat> implements ItemcatService { 
   

    @Override
    public List<Itemcat> getItemCatList() { 
   
        //查询分类类别 [即先查一级分类]
        List<Itemcat> list = getItems(0);
        return list;
    }

    private List<Itemcat> getItems(long parentId) { 
   

        //根据parentId查询分类列表[限制条件]
        Itemcat itemcat = new Itemcat();
        itemcat.setParentId(parentId);
        List<Itemcat> list = this.findByWhere(itemcat);

        //判断分类列表是否有数据
        if (list != null && list.size() > 0) { 
   
            //如果有数据再次根据分类id查询分类列表(使用递归)
            list.forEach(c -> c.setChildren(getItems(c.getId())));
        } else { 
   
            //如果没有数据,直接返回null[结束递归]
            return null;
        }
        //最后返回分类列表
        return list;
    }
}

controller
代码语言:javascript
复制
package com.supergo.controller;

import com.supergo.http.HttpResult;
import com.supergo.pojo.Itemcat;
import com.supergo.service.ItemcatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-23 19:25 * @Modified By: */
@RestController
@RequestMapping("/cat")
public class ItemcatController { 
   

    @Autowired
    private ItemcatService itemCatService;

    //分类递归查询
    @PostMapping("/category")
    public HttpResult getItemCatList() { 
   
        List<Itemcat> list = itemCatService.getItemCatList();
        return HttpResult.ok(list);
    }
}

supergo_manager_feign

添加到 feign 接口中去

代码语言:javascript
复制
package com.supergo;

import com.supergo.http.HttpResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-23 21:24 * @Modified By: */
@FeignClient(value = "supergo-manager")//发现服务提供者
public interface ApiItemcatFeign { 
   

    //分类递归查询
    @PostMapping("/cat/category") //feign服务使用controller层时,要将根请求路径写进来
    public HttpResult getItemCatList();
}

3、supergo_portal9002 门户微服务

由于首页是系统中非常重要的一个页面,所以需要创建一个独立的模块来专门处理首页的业务逻辑

1、建 Module :supergo_portal9002
2、改 pom
代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>supergo_parent</artifactId>
        <groupId>com.supergo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>supergo_portal9002</artifactId>
    
    <dependencies>
        <!--eureka 客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--hystrix熔断依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--在feign模块使用了tk-mybatis进行数据库的访问, 这里引入feign,会间接引入tk-mybatis 需要排除tk-mybatis,不然会要求进行数据库配置 -->
        <dependency>
            <groupId>com.supergo</groupId>
            <artifactId>supergo_manager_feign</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>tk.mybatis</groupId>
                    <artifactId>mapper-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- swagger:前端访问的服务,使用swagger生成相应的接口文档说明 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <!--Swagger ui组件:和swagger用的同一套json数据,只是其展示的页面结构更加清晰-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.3</version>
        </dependency>
        <!-- 工具类 -->
        <dependency>
            <groupId>com.supergo</groupId>
            <artifactId>supergo-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 加入 redis 支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
3、建 yml
代码语言:javascript
复制
# 端口号【openfeign 也是使用在客户端,集成了ribbon】
server:
  port: 9002
spring:
  application:
    name: supergo-portal9002
  # 配置redis
# redis:
# host: 127.0.0.1
# password: 123456
eureka:
  client:
    register-with-eureka: true # 注册进注册中心
    service-url:
      # 集群版
      # defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
      # 单机版
      defaultZone: http://eureka7001.com:7001/eureka/
    # 显示的服务主机名称
  instance:
    prefer-ip-address: true # 访问路径显示 ip【统一:方便调试】
    ip-address: 127.0.0.1
    instance-id: ${ 
   eureka.instance.ip-address}.${ 
   server.port}
    lease-renewal-interval-in-seconds: 3
    lease-expiration-duration-in-seconds: 10


#actuator服务监控与管理
management:
  endpoint:
    #开启端点
    shutdown:
      enabled: true
    health:
      show-details: always
  # 加载所有的端点
  endpoints:
    web:
      exposure:
        include: "*"

# 设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
  # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  # 指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000
logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.supergo.ApiBrandFeign: debug
4、启动类
代码语言:javascript
复制
package com.supergo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-25 17:12 * @Modified By: */
@SpringBootApplication
@EnableEurekaClient //注册进注册中心
@EnableFeignClients //发现feign服务
public class PortalApplication9002 { 
   
    public static void main(String[] args) { 
   
        SpringApplication.run(PortalApplication9002.class, args);
    }
}
5、Controller
代码语言:javascript
复制
package com.supergo.controller;

import com.supergo.ApiItemcatFeign;
import com.supergo.http.HttpResult;
import com.supergo.pojo.Itemcat;
import com.supergo.user.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-25 17:14 * @Modified By: */
@RestController
public class ItemCatController { 
   

    @Autowired
    private ApiItemcatFeign apiItemcatFeign;

    @GetMapping("/categorys/categorys")
    public HttpResult getItemCatController() { 
   
        HttpResult result = apiItemcatFeign.getItemCatList();
        return result;
    }
}

4、supergo_zuul9999 网关微服务

增加匹配路由:portal-web ,当匹配到/portal/**路径时,调用 supergo-portal9002 微服务

代码语言:javascript
复制
# 端口
server:
  port: 9999
  # 用的上下文路径,也可以称为项目路径,是构成url地址的一部分。
  # 不配置时,默认为 /
  servlet:
    context-path: /api #服务端口
spring:
  application:
    name: supergo-zuul9999 #指定服务名

zuul:
  routes:
  #标识你服务的名字,这里可以自己定义,一般方便和规范来讲还是跟自己服务的名字一样
    manager-web:
      #服务映射的路径,通过这路径就可以从外部访问你的服务了,目的是为了不爆露你机器的IP,面向服务的路由了,给你选一个可用的出来,
      #这里zuul是自动依赖hystrix,ribbon的,不是面向单机
      path: /manager/** # 这里是映射路径
      #如 url:http://localhost:8080/ 这样的不好就是写死IP了,万一这IP挂了,这高可用性,服务注册那套东西就用不起来了
      serviceId: supergo-manager-web9001 #品牌服务的名称
    # 门户服务
    portal-web:
      path: /portal/**
      serviceId: supergo-portal9002 # 门户微服务
      #重试
      retryable: true
      sensitive-headers: #请求头跨系统发送token秘钥
      host:
        connect-timeout-millis: 50000
        socket-timeout-millis: 50000
        connection-request-timeout-millis: 50000
eureka:
  client:
    register-with-eureka: true # 表示将自己注册到 eureka server ,默认为 true
    fetch-registry: true # 表示是否从eureka server 抓取已有的注册信息,默认为true。单节点为所谓,集群必须为 true,才能配合ribbon使用负载均衡
    registry-fetch-interval-seconds: 5 # 获取服务列表的周期:5s
    service-url:
      # 单机版:只用注册进一个服务中心【defaultZone: http://127.0.0.1:7001/eureka/】
      defaultZone: http://eureka7001.com:7001/eureka/
      # 集群版:需要同时注册进每个注册中心
  # defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com/eureka/
  # 显示的服务主机名称
  instance:
    prefer-ip-address: true # 访问路径显示 ip【统一:方便调试】
    ip-address: 127.0.0.1
    instance-id: ${ 
   eureka.instance.ip-address}.${ 
   server.port}
    lease-renewal-interval-in-seconds: 3
    lease-expiration-duration-in-seconds: 10
    #网关设置了根路径,默认监控路径发生了变化
    health-check-url-path: /api/actuator/health

#actuator服务监控与管理
management:
  endpoint:
    #开启端点
    shutdown:
      enabled: true
    health:
      show-details: always
  # 加载所有的端点
  endpoints:
    web:
      exposure:
        include: "*"

5、前端配置

前端请求
在这里插入图片描述
在这里插入图片描述
效果
在这里插入图片描述
在这里插入图片描述

添加 Redis 缓存

在这里插入图片描述
在这里插入图片描述

前端门户展示的这些模块较多,并且用户还可能在不同的客户端,电脑浏览器、小程序等等同时访问,其并发量可想而知,为增加其访问速度,我们可将这些模块放在 redis ,当第一次访问后便放在里面,以后每次再拿取相同的东西,直接从redis 中取即可

1、改 pom

代码语言:javascript
复制
<!-- 加入 redis 支持 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、改 yml

代码语言:javascript
复制
spring:
  # 配置redis
  redis:
  	# 主机
    host: 127.0.0.1
    # redis 密码
    password: 123456

3、改 Controller

代码语言:javascript
复制
package com.supergo.controller;

import com.supergo.ApiItemcatFeign;
import com.supergo.http.HttpResult;
import com.supergo.pojo.Itemcat;
import com.supergo.user.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-25 17:14 * @Modified By: */
@RestController
public class ItemCatController { 
   

    @Autowired
    private ApiItemcatFeign apiItemcatFeign;

    //springboot集成redis提供的一个模板实例
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/categorys/categorys")
    public HttpResult getItemCatController() { 
   

        //查询商品分类之前可以先查询缓存
        String itemJson = redisTemplate.opsForValue().get("portal-itemCats");

        //如果缓存中有数据,直接返回
        if(StringUtils.isNotBlank(itemJson)){ 
   
            //反序列化可以是对象或者集合,注意你序列号进去的是什么
            HttpResult result = JsonUtils.jsonToPojo(itemJson, HttpResult.class);
            return result;
        }
        //如果缓存中没有数据,查询数据
        HttpResult result = apiItemcatFeign.getItemCatList();
        //将查询到的数据放到缓存
        redisTemplate.opsForValue().set("portal-itemCats",JsonUtils.objectToJson(result));
        return result;
    }
}

4、启动 redis

我们这里先暂时使用 windows 下的 redis ,进入 redis 安装目录,

打开 cmd,进入到刚才解压到的目录,启动临时服务:redis-server.exe redis.windows.conf (备注:通过这个命令,会创建Redis临时服务,不会在window Service列表出现Redis服务名称和状态,此窗口关闭,服务会自动关闭。)

在这里插入图片描述
在这里插入图片描述

5、测试

刷新前端页面,到 redis 客户端发现增加了缓存数据

在这里插入图片描述
在这里插入图片描述

你也可以在往 redis 增加的地方打断点测试一下


广告内容展示

操作也是和分类方式一样

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152828.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求
  • 实现
    • 1、pojo 实体类
      • 2、supergo_manager 微服务
        • supergo_manager_service8001
        • supergo_manager_feign
      • 3、supergo_portal9002 门户微服务
        • 1、建 Module :supergo_portal9002
        • 2、改 pom
        • 3、建 yml
        • 4、启动类
        • 5、Controller
      • 4、supergo_zuul9999 网关微服务
        • 5、前端配置
          • 前端请求
          • 效果
      • 添加 Redis 缓存
        • 1、改 pom
          • 2、改 yml
            • 3、改 Controller
              • 4、启动 redis
                • 5、测试
                • 广告内容展示
                相关产品与服务
                云数据库 Redis
                腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档