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

7_首页分类按级别展示[通俗易懂]需求在首页展示商品分类列表属性值请求urlhttp://{zuul:port}/api/portal/categorys/categorys请求方式GET参数无返回值HttpResult.ok(分类列表)实现1、pojo实体类要展示商品分类信息,需要树形结构展示,所以需要在Itemcat类中增加一个childern属性表示子分类比如:我们查看一级目录有:家电、手机、服装,但在家电这个一级目录下,又有分类,我们称为二级目录,有:电视

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

需求

在首页展示商品分类列表

属性
请求 url http://{zuul:port}/api/portal/categorys/categorys
请求方式 GET
参数
返回值 HttpResult.ok(分类列表)

实现

1、pojo 实体类

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

在这里插入图片描述

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

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

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

2、supergo_manager 微服务

supergo_manager_service8001

Service

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

接口

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();
}

实现类

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
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 接口中去

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

<?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

# 端口号【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、启动类

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

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 微服务

# 端口
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

<!-- 加入 redis 支持 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、改 yml

spring:
  # 配置redis
  redis:
  	# 主机
    host: 127.0.0.1
    # redis 密码
    password: 123456

3、改 Controller

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.net/152828.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月25日 下午5:16
下一篇 2022年6月25日 下午5:36


相关推荐

  • startService bindService 区别「建议收藏」

    startService bindService 区别「建议收藏」Android执行Service有两种方法,一种是startService,一种是bindService。下面让我们一起来聊一聊这两种执行Service方法的区别。 1、生命周期上的区别执行startService时,Service会经历onCreate-&gt;onStartCommand。当执行stopService时,直接调用onDestroy方法。调用者如果没有stopService,Se…

    2022年5月25日
    37
  • SqlServer服务器和数据库角色

    SqlServer服务器和数据库角色

    2021年11月25日
    44
  • linux查看硬盘大小与挂载硬盘

    linux查看硬盘大小与挂载硬盘linux 查看硬盘大小与挂载硬盘 linux 的硬盘有挂载和未挂载两种 类似 windows 上的硬盘分区 C D E 盘 1 查看已经挂载的硬盘大小 df h 2 查看详细的硬盘分区情况 包括挂载和未挂载两种的硬盘大小 fdisk l 介绍 1 df 用于检查文件系统磁盘占用情况 2 du 检查磁盘空间占用情况 3 fdisk 用于磁盘分区详解 1 dfdf 命

    2026年3月19日
    1
  • caller和callee两者区别

    caller和callee两者区别caller 返回一个函数的引用 这个函数调用了当前的函数 callee 返回正在执行的函数本身的引用 它是 arguments 的一个属性 caller 注意 1 这个属性只有函数执行时才起作用 2 如果函数由顶层调用 则返回 null 例 1 functiona console log a caller functionb a

    2026年3月19日
    2
  • origin做上下柱状图_利用excel制作柱状图

    origin做上下柱状图_利用excel制作柱状图目标:在Origin8中作向下的柱状图(暂且这么称呼吧),类似下图形式:示例数据:方法1:最简便的方法——(1)将数据X、Y1生成条状图(“normal”);(2)菜单栏Window——ScriptWindow;(3)点击生成的Graph图层(激活状态);(4)在ScriptWindow输入layer1.y.r…

    2026年4月19日
    5
  • QSettings介绍

    QSettings介绍简介 QSettings 类提供了持久的跨平台应用程序设置 用户通常期望应用程序记住它的设置 窗口大小 位置等 所有会话 这些信息通常存储在 Windows 系统注册表 OSX 和 iOS 的属性列表文件中 在 Unix 系统中 在缺乏标准的情况下 许多应用程序 包括 KDE 应用程序 使用 INI 文本文件 QSettings 围绕这些抽象技术 使我们能够以便携的方式保存和恢复应用程序设置 它还支持自定义存储格式 QSet

    2026年3月19日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号