15.SpringBoot 整合 Redis

  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


    SpringBoot 整合 Redis


    简单介绍

    前面我们基本把Redis 的基本知识介绍完了,但是可以看出,全部都是在SSH命令行操作的,作为一个学习Java的程序员,肯定希望使用Java来操作一把。我们这里直接使用SpringDataRedis来玩,不在单独使用Jedis来操作了。

    项目搭建

    创建项目

    创建项目我们使用 SpringBoot的引导器来创建,这里就不多赘述了。如何创建SpringBoot项目可以参考。


    导入依赖

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.10.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.10.2</version>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    依赖分为两个部分,第一个部分就是​​jackson-databind,jackson-core,jackson-annotations​​​ 关于jackson的几个,第二个部分就是SpringBoot整合redis的stater​​spring-boot-starter-data-redis​​ 。

    配置文件

    spring.redis.host=127.0.0.1
    # Redis服务器连接端口
    spring.redis.port=6379
    # Redis数据库索引(默认为0)
    spring.redis.database=0
    # Redis服务器连接密码(默认为空)
    spring.redis.password=

    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.jedis.pool.max-idle=8
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.jedis.pool.max-wait=-1
    # 连接池中的最大空闲连接数
    spring.redis.jedis.pool.max-active=8
    # 连接池中的最小空闲连接数
    spring.redis.jedis.pool.min-idle=0
    # 连接超过时间(毫秒)
    #spring.redis.timeout=0

    配置类

    package com.ouyanghao.springbootredis.config;

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    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.core.StringRedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;

    @Configuration
    public class RedisConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){

    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

    RedisTemplate<String,Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());//key的序列化方式
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
    }


    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory){
    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    stringRedisTemplate.setConnectionFactory(factory);
    return stringRedisTemplate;
    }
    }

    封装组件类

    package com.ouyanghao.springbootredis.test;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;

    /**
    * 主要用于测试相应的List类型的API
    */
    @Component
    public class RedisListOps {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public void leftPush(String key, Object value){
    redisTemplate.boundListOps(key).leftPush(value);
    }

    public void leftPushAll(String key, Object...value){
    redisTemplate.boundListOps(key).leftPushAll(value);
    }

    public Object leftPop(String key){
    return redisTemplate.boundListOps(key).leftPop();
    }
    }

    这个组件类是以操作List列表创建的,就不单独每一种类型都创建一个了。

    项目测试

    测试代码

    package com.ouyanghao.springbootredis;

    import com.ouyanghao.springbootredis.test.RedisListOps;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;

    @SpringBootTest
    class SpringbootRedisApplicationTests {

    @Autowired
    private RedisListOps redisListOps;

    @Test
    void contextLoads() {
    redisListOps.leftPush("animal","apple");
    redisListOps.leftPushAll("animal","orange","banana");
    // System.out.println(redisListOps.leftPop("animal"));
    // System.out.println(redisListOps.leftPop("animal"));
    // System.out.println(redisListOps.leftPop("animal"));
    }
    }

    测试结果

    15.SpringBoot 整合 Redis_spring

    使用SpringBoot整合Redis就先到这里了。


  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
    标签: Springredis