1. 添加依赖
在pom.xml
中添加必要的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
2. 配置Redis
在application.properties
或application.yml
中配置Redis:
spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=yourpassword
3. 创建Redis配置类
创建一个Redis配置类,用于配置RedisTemplate
:
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.serial