这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis。

引入依赖:

在pom文件中添加redis依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置数据源

spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=
spring.redis.database=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0

如果你的redis有密码,配置下即可。经过上述两步的操作,你可以访问redis数据了。

数据访问层dao

通过redisTemplate来访问redis.

@Repository
public class RedisDao {@Autowiredprivate StringRedisTemplate template;public  void setKey(String key,String value){ValueOperations<String, String> ops = template.opsForValue();ops.set(key,value,1, TimeUnit.MINUTES);//1分钟过期}public String getValue(String key){ValueOperations<String, String> ops = this.template.opsForValue();return ops.get(key);}
}

单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {public static Logger logger= LoggerFactory.getLogger(SpringbootRedisApplicationTests.class);@Testpublic void contextLoads() {}@AutowiredRedisDao redisDao;@Testpublic void testRedis(){redisDao.setKey("name","forezp");redisDao.setKey("age","11");logger.info(redisDao.getValue("name"));logger.info(redisDao.getValue("age"));}
}

启动单元测试,你发现控制台打印了:

forezp

11

单元测试通过;

源码下载:https://github.com/forezp/SpringBootLearning

参考资料

messaging-redis

SpringBoot第九篇: springboot整合Redis相关推荐

  1. redis序列化_实例讲解Springboot以Template方式整合Redis及序列化问题

    1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...

  2. 继承redis spring_实例讲解Springboot以Repository方式整合Redis

    1 简介 Redis是高性能的NoSQL数据库,经常作为缓存流行于各大互联网架构中.本文将介绍如何在Springboot中整合Spring Data Redis,使用Repository的方式操作. ...

  3. mysql springboot 缓存_Spring Boot 整合 Redis 实现缓存操作

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 一.缓 ...

  4. springboot获取sessionid_Spring Boot 整合Redis, 用起来真简单!

    点击上方"Java技术前线",选择"置顶或者星标" 与你一起成长- 作者:java_老男孩  https://blog.51cto.com/14230003/2 ...

  5. springboot篇】二十二. springboot整合Redis集群

    springboot整合Redis集群 **中国加油,武汉加油!** 案例准备 1. 新建Springboot项目springboot-redis-sentinel 2. 编写yml 3. Test ...

  6. springboot整个缓存_SpringBoot中整合Redis(缓存篇)

    实际开发中缓存处理是必须的,不可能我们每次客户端去请求一次服务器,服务器每次都要去数据库中进行查找,为什么要使用缓存?说到底是为了提高系统的运行速度.将用户频繁访问的内容存放在离用户最近,访问速度最快 ...

  7. Redis学习篇3_事务及其监控(锁)、Jedis、SpringBoot整合Redis、RedisTemplate的json序列化、RedisUtil工具类

    目录 事务及其监控(锁) Jedis SpringBoot整合Redis RedisTemplate 默认RedisTemplate来源 关于中文序列化问题 RedisUtil工具类 一.事务及其监控 ...

  8. SpringBoot(六):SpringBoot整合Redis

    From: https://blog.csdn.net/plei_yue/article/details/79362372 前言 在本篇文章中将SpringBoot整合Redis,使用的是RedisT ...

  9. springboot整合redis,推荐整合和使用案例(2021版)

    背景:手下新人在初次使用springboot整合redis,大部分人习惯从网上检索到一份配置,然后不知其所以然的复制粘贴到项目中,网上搜索到的配置良莠不齐但又万变不离其宗.由于springboot最大 ...

最新文章

  1. 火出圈!河南大学教授毕业典礼金句频现:躺平得了初一,躺平不到十五!
  2. java 检查输入_在java中检查输入的问题
  3. 线段树总结(一)【数据结构】
  4. visual studio报错:error C4996: ‘scanf‘
  5. NoSql数据库:Cassandra,Mongo,Redis数据库比较
  6. hibernate批量查询_使用Hibernate批量获取
  7. systemctl自定义service
  8. 喜欢网络文学的人有多少?当代年轻人是这样阅读的
  9. 中文计算机语言编程优势,十种编程语言特点比较
  10. 测量string变量长度函数_C语言中测试字符串长度的函数
  11. 中国石油大学《输气管道设计与管理(含课程设计)》第三阶段在线作业
  12. tensorflow graphics详解
  13. kaka的使用以及理解
  14. 安装虚拟机时总是会回滚
  15. 电影html css页面,纯CSS3构建的电影屏幕效果
  16. 一般纳税人有限公司可以享受的税收优惠政策有哪些?
  17. Laravel数据库之Seeding
  18. 提效篇 |当项目紧急入场,如何先测量后校正?
  19. The page cannot be refreshed without resending ... 昨天遇到一个这样得问题,在弹出的子页中用:
  20. fffffffffffffffffffffffffffffffff

热门文章

  1. BZOJ——1202: [HNOI2005]狡猾的商人
  2. 神经网络二(Neural Network)
  3. tomcat在服务器上改了8080的端口之后所带来的问题
  4. 快速入门linux系统的iptables防火墙 1 本机与外界的基本通信管理
  5. delphi对窗体的查询(delphi xe2)
  6. C#中的委托和事件 (4)---事件和委托的编译代码
  7. 【组队学习】【32期】动手学数据分析
  8. Software development Problem
  9. 【MATLAB】数据分析之多项式及其函数
  10. TIOBE 新榜单:Python 超越 Java 重回第二,Rust 崛起