springboot如何整合redis
时间: 2023-08-29 09:13:33 浏览: 83
Spring Boot可以通过使用Spring Data Redis模块来轻松地与Redis进行集成。下面是整合步骤:
1.添加依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
2.配置Redis连接信息
在application.properties文件中添加以下配置信息:
```
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
```
3.使用RedisTemplate操作Redis
在需要使用Redis的类中,注入RedisTemplate对象,即可使用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);
}
```
以上就是Spring Boot整合Redis的步骤。
阅读全文
相关推荐










