一、pom.xml引入redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、在application.yml文件中配置redis
redis:
# Redis数据库索引(默认为0)
database: 0
host: localhost
port: 6379
#密码默认空,有则配置上
password:
# 连接超时时间(毫秒)
timeout: 1000
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 200
# 连接池中的最大空闲连接
max-idle: 10
# 连接池中的最小空闲连接
min-idle: 0
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
三、在项目的test下创建RedisConfigTest,测试链接是否成功
注意:springbootTest中配置的为项目运行main方法所在类的类名。也就是注解@SpringBootApplication所修饰的类。
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class RedisConfigTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test1(){
redisTemplate.opsForValue().set("aaa","111");
log.info("成功---"+redisTemplate.opsForValue().get("aaa"));
}
}
输出结果:
四、使用RedisDesktopManager工具查看结果
查询,成功,已经缓存至redis中。