SpringBoot整合redis

序言

  • SpringBoot 操作数据的有:spring-data jpa,jdbc ,mongodb ,redis
  • 项目中必不可少redis,它是非关系型数据库,有很多应用场景,如存储个人信息,最近浏览,评论等等,更重要的是,用redis当做缓存,可以大大减轻数据库访问的压力,提高访问效率。java操作redis, 在 SpringBoot2.x 之后,原来使用的jedis 被替换为了 lettuce
  1. jedis : 底层采用的直连,如果多个线程操作的话是不安全的,如果想要避免不安全的,使用 jedis pool 连接池,这就更像 BIO 模式。
  2. lettuce : 采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据了,更像 NIO模式

配置步骤

1.引入依赖

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

2.在application.yml中配置连接

spring:
  #配置redis
  redis:
    host: 127.0.0.1
    port: 6379

3.将本地的reids服务端打开之后,就可以在java代码中使用jedis来操作了,如下简单的set/get测试

@SpringBootTest
class SpringbootApplicationTests {
	
	@Autowired
	private RedisTemplate redisTemplate;

	void testReids(){
		redisTemplate.opsForValue().set("key1","value1");
		redisTemplate.opsForValue().get("key1");
	}
}

4.我们需要注意的一个问题是: 默认的序列化方式是JDK序列化,我们会使用json来序列化,关于对象的保存:所有的对象,都需要序列化,所以我们需要自己定义了一个 RedisTemplate,用的时候就用自己定义的模版就好了,不用担心对象序列化的问题了。

package com.daq.Springboot.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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        // 为了开发方便,一般直接使用 <String, Object>
        RedisTemplate<String, Object> template=new RedisTemplate<String,Object>();
        template.setConnectionFactory(factory);
        // Json序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om=new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // String 的序列化
        StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

好的,下面是SpringBoot整合Redis的步骤: 1. 在pom.xml中引入Redis的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties或application.yml中配置Redis连接信息,例如: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 ``` 3. 创建RedisConfig类,配置RedisTemplate和StringRedisTemplate: ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(redisConnectionFactory); return stringRedisTemplate; } } ``` 4. 在需要使用Redis的地方注入RedisTemplate或StringRedisTemplate,并使用相应方法操作Redis,例如: ``` @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } ``` 以上就是SpringBoot整合Redis的基本步骤,希望能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值