java sprintboot 集成 redis
时间: 2023-07-06 10:34:47 浏览: 95
好的,关于Java Spring Boot集成Redis,我可以简单地给您介绍一下步骤:
1.在pom.xml中添加redis依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
2.在application.properties中配置Redis连接信息:
```properties
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
```
3.在配置类中添加RedisTemplate的bean:
```java
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
```
4.在需要使用Redis的地方注入RedisTemplate并使用即可:
```java
@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);
}
```
以上就是Java Spring Boot集成Redis的简单步骤,希望能对您有所帮助。
阅读全文