网友回复
在Spring Boot中整合Redis,可以使用Spring Data Redis。以下是一个简单的示例,展示了如何配置和使用Redis。
1. 添加依赖在pom.xml文件中添加Spring Data Redis和Lettuce(Redis客户端)的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce.core</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
</dependencies> 2. 配置Redis连接在src/main/resources/application.properties文件中添加Redis的连接配置:
spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=yourpassword # 如果没有密码,可以省略此行3. 创建Redis配置类(可选)
如果需要更复杂的配置,可以创建一个配置类:
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Be...点击查看剩余70%


