SpringBoot整合Redis

详解SpringBoot整合Redis

版权声明:本文为转载文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36781505/article/details/86612988

SpringBoot整合Redis

在SpringBoot中一般使用RedisTemplate提供的方法来操作Redis。那么使用SpringBoot整合Redis
需要那些步骤呢。

  • 1.JedisPoolConfig(这个是配置连接池)

  • 2.RedisConnectionFactory(这个是配置连接信息,这里的RedisConnectionFactory是一个接口,
    我们需要使用它的实现类,在SpringD Data Redis方案中提供了一下四种工厂模型)

    • JredisConnectionFactory
    • JedisConnectionFactory
    • LettuceConnectionFactory
    • SrpConnectionFactory
  • 3.RedisTemplate

1、添加启动器

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

     
     
  • 1
  • 2
  • 3
  • 4
  • 5

2、配置连接信息

spring:
    redis:
      host: 127.0.0.1 
      port: 6379
      password: 123456
      jedis:
        pool:
          max-active: 8
          max-wait: -1
          max-idle: 500
          min-idle: 0
      lettuce:
        shutdown-timeout: 0

     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3、测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test_1{
    @Autowired
    private RedisTemplate<String,String>redisTemplate;
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">set</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">"myKey"</span><span class="token punctuation">,</span><span class="token string">"myValue"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"myKey"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4、查看结果

在这里插入图片描述

5、序列化器

这里先说一下Spring提供了那些序列化器(各自作用自行百度,这里不再赘述)
Jackson2JsonRedisSerializer
JdkSerializationRedisSerializer
OxmSerializer
StringRedisSerializer
GenericToStringRedisSerializer
GenericJackson2JsonRedisSerializer

然后以下是一些问题需要搞清楚

  • 为什么需要序列化?
    你要记住一句话,在JAVA中,一切皆对象,而将对象的状态信息转为存储或传输的形式需要序列化。

  • 为什么前面的代码没有设置序列化?
    原因是是因为我们使用了@AutoWired,IOC容器为我们选择了StringRedisTemplate类来注入
    以下是单步调试的截图
    在这里插入图片描述
    这里是RedisTemplate的继承关系图
    在这里插入图片描述
    然后我们跟踪源码可以发现
    StringRedisTemplate.java

	public StringRedisTemplate() {
		setKeySerializer(RedisSerializer.string());
		setValueSerializer(RedisSerializer.string());
		setHashKeySerializer(RedisSerializer.string());
		setHashValueSerializer(RedisSerializer.string());
	}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

RedisSerializer.java

static RedisSerializer<String> string() {
		return StringRedisSerializer.UTF_8;
	}

 
 
  • 1
  • 2
  • 3

StringRedisSerializer.java

public static final StringRedisSerializer UTF_8 = new StringRedisSerializer(StandardCharsets.UTF_8);

 
 
  • 1

StringRedisTemplate默认选择的StringRedisSerializer序列化器

  • 那假如我们把Value的类型改为Object呢
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test_1{
    @Resource
    private RedisTemplate<String,Object>redisTemplate;
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">set</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">"myKey"</span><span class="token punctuation">,</span><span class="token string">"myValue"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"myKey"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

注意:这里继续使用@AutoWired会报错,需要使用@Resource,这两个注解的区别在前者是根据类型后者
是根据名字,具体区别自行查资料(这里说一下为什么会报错:@AutoWired找不到该类型
<String,Object>的Bean因为根本没有。使用@Resource直接注入的是RedisTemplate)

在这里插入图片描述
然后运行代码,我们去看RedisManeger
在这里插入图片描述
咦?怎么会这样?
这是因为没有设置序列化器,RedisTemplate选择了默认的序列化器JdkSerializationRedisSerializer
官方文档是这样说的:
RedisTemplate (Spring Data Redis 2.1.4.RELEASE API)

Performs automatic serialization/deserialization between the given objects and
the underlying binary data in the Redis store. By default, it uses Java serialization
for its objects (through JdkSerializationRedisSerializer ).
For String intensive operations consider the dedicated StringRedisTemplate.

6、配置序列化器

说了这么多,那么我们如何自己设置序列化器呢,方法有很多种,比如比较简单的就是
方法1

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test_1{
    @Resource
    private RedisTemplate<String,Object>redisTemplate;
    @Test
    public void set(){
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setKeySerializer(RedisSerializer.string());
        //或者
        //redisTemplate.setKeySerializer(new StringRedisSerializer());
        //redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set("myKey","myValue");
        System.out.println(redisTemplate.opsForValue().get("myKey"));
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

有的时候每次这样设置比较烦人,那么就采用配置类(更多的时候采用这种方法)
方法2(这样的话,测试类就应该使用@AutoWired了哦)

@Configuration
public class RedisConfig{
    @Bean
    public RedisTemplate<String,Object>redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object>template=new RedisTemplate<>();
        //关联
        template.setConnectionFactory(factory);
        //设置key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化器
         template.setValueSerializer(new StringRedisSerializer());
        return template;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

为什么说更多的时候采用这种方法呢,实际情况中可能有多种需求,泛型各不相同,比如有<String,User>
直接在Config类里面在加一个方法就行。

7、完整例子(将实体类以JSON的形式储存到Redis中)

1、先给出entity(记住实体要序列化一定要实现Serializable接口)

@Data
public class Days implements Serializable {
    private String openId;
    private String daysId;
    //每天的标题
    private String title;
    //代办事项的数量
    private int itemNumber;
    //日程
    private String date;
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、然后在配置类中设置序列化器

@Configuration
public class RedisConfig{
    @Bean
    public RedisTemplate<String, Days>redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Days>template=new RedisTemplate<>();
        //关联
        template.setConnectionFactory(factory);
        //设置key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化器
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Days.class));
        return template;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3、测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> RedisTemplate<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span>Days<span class="token punctuation">&gt;</span></span> redisTemplate<span class="token punctuation">;</span>
<span class="token keyword">private</span> Days d<span class="token punctuation">;</span>
<span class="token annotation punctuation">@Before</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">before</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    d<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Days</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    d<span class="token punctuation">.</span><span class="token function">setDate</span><span class="token punctuation">(</span><span class="token string">"123"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    d<span class="token punctuation">.</span><span class="token function">setDaysId</span><span class="token punctuation">(</span><span class="token string">"456"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    d<span class="token punctuation">.</span><span class="token function">setItemNumber</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    d<span class="token punctuation">.</span><span class="token function">setOpenId</span><span class="token punctuation">(</span><span class="token string">"dawda"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    d<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">"days"</span><span class="token punctuation">,</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">(</span>redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"days"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

4、运行结果
在这里插入图片描述
很激动有木有!(o)/~

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
                </div>
</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值