前言
如果计划使用SpringBoot连接Redis, 请先配置好使用的redis,并建立基础的SpringBoot工程。
maven中心服务器https://repo.maven.apache.org/maven2/
正文
配置Redis
pom.xml加入dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
application.yml加入redis配置
spring:
redis:
host: 127.0.0.1 #host
port: 6379 #port
database: 10 #使用的数据库(默认0-15选择其中一个)
timeout: 5000 #连接超时时间
jedis:
pool:
max-active: 50 #最大连接数
max-wait: 3000 #阻塞时最大等待时间
max-idle: 20 #最大空闲连接
min-idle: 2 #最小空闲连接
使用
有两种使用方式:
- 作为一种数据库,直接当做数据库来使用
- 作为缓存使用(通过注解)
数据库(以下只针对String => String的方式)
编写基础方法
@Component
public class RedisConfig {
private static final Logger log = LoggerFactory.getLogger(RedisConfig.class