SpringBoot怎么连接redis
作者:吹吹风会发光
时间:2023-04-26
来源:互联网
浏览:0
首先开启redis服务器:然后在springboot里面添加配置文件:#Redis数据库索引(默认为0)spring.redis.database=0#Redis服务器地址spring.redis.host=localhost#Redis服务器连接端口spring.redis.port=6379#Redis服务器连接密码(默认为空)spring.redis.password=#连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=8#连接池最大阻塞等待时间(使用
首先开启redis服务器:

然后在springboot里面添加配置文件:
# Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=localhost # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.timeout=0
最后在springboot的pom.xml文件中添加依赖:
org.springframework.boot spring-boot-starter-data-redis
然后在springboot的项目中写一个测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ShoppingApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void test() throws Exception {
List list =new ArrayList<>();
list.add("a");
list.add("b");
list.add("v");
stringRedisTemplate.opsForValue().set("abc", "测试");
stringRedisTemplate.opsForList().leftPushAll("qq",list);
// 向redis存入List
stringRedisTemplate.opsForList().range("qwe",0,-1).forEach(value ->{
System.out.println(value);
}
);
} 然后在redis里面便可以看到了刚才的新建的

然后这就是最简单的springboot连接redis的方式了
作者最新文章
PDF怎么取消密码保护?4种解锁方法整理
2026-09-08 18:23
三星 Galaxy Z Fold8 内屏边角支撑偏软?实测与官方回应
2026-09-08 16:39
手机Excel表格制作教程:小屏幕高效录入与格式调整指南
2026-09-04 09:27
PDF文档按页转换成图片怎么做?在线转换步骤整理
2026-09-03 11:06
PDF转Excel操作指南:在线工具、Adobe与Excel内置功能详解
2026-09-02 19:08
上一篇:
Java递归和迭代区别是什么
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















