• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot初步连接redis详解

springboot 搞代码 4年前 (2022-01-05) 64次浏览 已收录 0个评论

这篇文章主要介绍了SpringBoot初步连接redis详解,具有一定借鉴价值,需要的朋友可以参考下。

在初次用springboot连接redis的时候查看官方文档和一些博客会发现配置文件非常的多,这就导致了在学习的开始的时候是没有体验的,其实利用springboot连接redis的时候并不需要那么多的配置

首先开启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 # 连接超时时间(毫秒) <strong style="color:transparent">来源gaodai#ma#com搞@@代~&码*网</strong>spring.redis.timeout=0

最后在springboot的pom.xml文件中添加依赖:

  org.springframework.bootspring-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的方式了

总结

以上就是本文关于SpringBoot初步连接redis详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

SpringBoot优化技巧

Spring Boot 微服务项目的推荐部署方式

Springboot之于Spring的优势

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

以上就是SpringBoot初步连接redis详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot初步连接redis详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址