SpringBoot(三)——整合redis

springboot2.3.2+redis5.0.9

1.依赖包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

 

2.配置application.yml

spring:
  redis:
    #指定数据库,默认为0,在黑窗口里通过 select idx切换
    database: 0
    port: 6379
    host: 127.0.0.1
    #redis密码,默认为空,这里没有设置所以注释掉了
    #password: root
    pool:
      max-active: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
      max-wait: -1
        # 连接池中的最大空闲连接
      max-idle: 8
        # 连接池中的最小空闲连接
      min-idle: 0

 

3.配置RedisTemplate

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //使用fastjson序列化
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

这个当作是一个模板套用,如果不使用那些序列化,会使得【对Java的redisTemplate设置key,但是在redis中的key有一些乱七八糟的编码】,例如在Java中设置name,在redis中却这样显示。

RedisTemplate和StringRedisTemplate有一些区别,一般RedisTemplate就够用了

4.测试

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

@SpringBootTest
class DemoApplicationTests {

    @Resource
    RedisTemplate redisTemplate;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Test
    void testRedis() {
        redisTemplate.opsForValue().set("person","shoulinniao");
        redisTemplate.opsForValue().set("id",10086);
        System.out.println(redisTemplate.opsForValue().get("person"));
        System.out.println(redisTemplate.opsForValue().get("id"));
        System.out.println(stringRedisTemplate.opsForValue().get("person"));
        System.out.println(stringRedisTemplate.opsForValue().get("id"));
    }
}
/*输出
shoulinniao
10086
"shoulinniao"
10086
*/

 

在redis黑窗口也同步更新

 

posted @ 2020-08-20 11:24  守林鸟  阅读(457)  评论(0编辑  收藏  举报