SpringBoot连接redis
简介:本文讲解,如何使用SpringBoot连接redis.
如何下载redis看这篇文章:redis安装教程
项目源文件,在这个地址:https://gitee.com/geek-li-hua/code-in-blog.git
连接方法
- 首先启动redis
- 编写配置文件
这里需要修改的就是自己的密码
spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=root@123456 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.max-idle=500 spring.redis.jedis.pool.min-idle=0 spring.redis.lettuce.shutdown-timeout=0
- 编写测试类进行测试
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; @SpringBootTest class RedisDemoApplicationTests { @Autowired private StringRedisTemplate stringRedisTemplate; @Test void contextLoads() { stringRedisTemplate.opsForValue().set("xiaocai", "888"); String res = stringRedisTemplate.opsForValue().get("xiaocai"); System.out.println(res); } }