SpringBoot集成使用Redis及搭建过程

目录

  • SpringBoot集成使用redis
  • 搭建
    • 1.导入jar包
    • 2.配置连接redis
    • 3.添加配置类RedisConfig
    • 4.注入RedisTemplate
    • 5.测试和使用
  • 使用实例:

    SpringBoot集成使用redis Jedis 是 Redis 官方推出的一款面向 Java 的客户端,提供了很多接口供 Java 语言调用。可以在 Redis 官网下载. Spring-data-redis 是 spring 大家族的一部分,提供了在 srping 应用中通 过简单的配置访问 redis 服务,对 reids 底层开发包(Jedis, JRedis, and RJC)进 行了高度封装,RedisTemplate 提供了 redis 各种操作
    spring-data-redis 针对 jedis 提供了如下功能:
    1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类.
    2. 针对 jedis 客户端中大量 api 进行了归类封装,将同一类型操作封装为 operation 接口.
    ValueOperations:简单 K-V 操作
    SetOperations:set 类型数据操作
    ZSetOperations:zset 类型数据操作
    HashOperations:针对 map 类型的数据操作
    ListOperations:针对 list 类型的数据操作
    3.将事务操作封装,有容器控制。
    4.针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)
    JdkSerializationRedisSerializer:POJO 对象的存取场景,使用 JDK 本身 序列化机制.
    StringRedisSerializer:Key 或者 value 为字符串的场景,根据指定的charset 对数据的字节序列编码成 string,是“new String(bytes, charset)”和 “string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。
    JacksonJsonRedisSerializer:jackson-json 工具提供了 javabean 与 json 之 间的转换能力,可以将 pojo 实例序列化成 json 格式存储在 redis 中,也可以将 json 格式的数据转换成 pojo 实例。

    搭建
    1.导入jar包
    SpringBoot集成使用Redis及搭建过程
    文章图片

    org.springframework.bootspring-boot-starter-data-redis

    SpringBoot集成使用Redis及搭建过程
    文章图片


    2.配置连接redis
    SpringBoot集成使用Redis及搭建过程
    文章图片

    SpringBoot集成使用Redis及搭建过程
    文章图片

    spring: redis: host: 192.168.31.100 port: 6379password: 111 database: 0 pool: max-active: 8 # 连接池最大连接数(使用负值表示没有限制) max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)max-idle: 8 # 连接池中的最大空闲连接 min-idle: 0 # 连接池中的最小空闲连接 timeout: 5000ms # 连接超时时间(毫秒)

    在application.yml文件中spring下添加如上配置

    3.添加配置类RedisConfig
    SpringBoot集成使用Redis及搭建过程
    文章图片

    package com.ffyc.back.demo.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configurationpublic class RedisConfig {/*** 序列化键,值* @param connectionFactory* @return*/@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); StringRedisSerializer redisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(redisSerializer); redisTemplate.setHashKeySerializer(redisSerializer); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); return redisTemplate; }}
    在配置包中添加此配置
    此配置类的作用是将后端将要传过去的数据json序列化,如果没有此配置的话后端传过去的格式和redis端的不符合会出现乱码情况

    4.注入RedisTemplate
    SpringBoot集成使用Redis及搭建过程
    文章图片

    SpringBoot集成使用Redis及搭建过程
    文章图片

    SpringBoot集成使用Redis及搭建过程
    文章图片

    【SpringBoot集成使用Redis及搭建过程】在需要使用的地方注入 注入后就可以使用了

    5.测试和使用
    SpringBoot集成使用Redis及搭建过程
    文章图片


    使用实例: (1)
    SpringBoot集成使用Redis及搭建过程
    文章图片

    (2)
    SpringBoot集成使用Redis及搭建过程
    文章图片

    到此这篇关于SpringBoot集成使用Redis的文章就介绍到这了,更多相关SpringBoot集成使用Redis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

      推荐阅读