SpringBoot第九篇: springboot整合Redis
这篇文章主要介绍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相关推荐
- redis序列化_实例讲解Springboot以Template方式整合Redis及序列化问题
1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...
- 继承redis spring_实例讲解Springboot以Repository方式整合Redis
1 简介 Redis是高性能的NoSQL数据库,经常作为缓存流行于各大互联网架构中.本文将介绍如何在Springboot中整合Spring Data Redis,使用Repository的方式操作. ...
- mysql springboot 缓存_Spring Boot 整合 Redis 实现缓存操作
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 一.缓 ...
- springboot获取sessionid_Spring Boot 整合Redis, 用起来真简单!
点击上方"Java技术前线",选择"置顶或者星标" 与你一起成长- 作者:java_老男孩 https://blog.51cto.com/14230003/2 ...
- springboot篇】二十二. springboot整合Redis集群
springboot整合Redis集群 **中国加油,武汉加油!** 案例准备 1. 新建Springboot项目springboot-redis-sentinel 2. 编写yml 3. Test ...
- springboot整个缓存_SpringBoot中整合Redis(缓存篇)
实际开发中缓存处理是必须的,不可能我们每次客户端去请求一次服务器,服务器每次都要去数据库中进行查找,为什么要使用缓存?说到底是为了提高系统的运行速度.将用户频繁访问的内容存放在离用户最近,访问速度最快 ...
- Redis学习篇3_事务及其监控(锁)、Jedis、SpringBoot整合Redis、RedisTemplate的json序列化、RedisUtil工具类
目录 事务及其监控(锁) Jedis SpringBoot整合Redis RedisTemplate 默认RedisTemplate来源 关于中文序列化问题 RedisUtil工具类 一.事务及其监控 ...
- SpringBoot(六):SpringBoot整合Redis
From: https://blog.csdn.net/plei_yue/article/details/79362372 前言 在本篇文章中将SpringBoot整合Redis,使用的是RedisT ...
- springboot整合redis,推荐整合和使用案例(2021版)
背景:手下新人在初次使用springboot整合redis,大部分人习惯从网上检索到一份配置,然后不知其所以然的复制粘贴到项目中,网上搜索到的配置良莠不齐但又万变不离其宗.由于springboot最大 ...
最新文章
- 火出圈!河南大学教授毕业典礼金句频现:躺平得了初一,躺平不到十五!
- java 检查输入_在java中检查输入的问题
- 线段树总结(一)【数据结构】
- visual studio报错:error C4996: ‘scanf‘
- NoSql数据库:Cassandra,Mongo,Redis数据库比较
- hibernate批量查询_使用Hibernate批量获取
- systemctl自定义service
- 喜欢网络文学的人有多少?当代年轻人是这样阅读的
- 中文计算机语言编程优势,十种编程语言特点比较
- 测量string变量长度函数_C语言中测试字符串长度的函数
- 中国石油大学《输气管道设计与管理(含课程设计)》第三阶段在线作业
- tensorflow graphics详解
- kaka的使用以及理解
- 安装虚拟机时总是会回滚
- 电影html css页面,纯CSS3构建的电影屏幕效果
- 一般纳税人有限公司可以享受的税收优惠政策有哪些?
- Laravel数据库之Seeding
- 提效篇 |当项目紧急入场,如何先测量后校正?
- The page cannot be refreshed without resending ... 昨天遇到一个这样得问题,在弹出的子页中用:
- fffffffffffffffffffffffffffffffff
热门文章
- BZOJ——1202: [HNOI2005]狡猾的商人
- 神经网络二(Neural Network)
- tomcat在服务器上改了8080的端口之后所带来的问题
- 快速入门linux系统的iptables防火墙 1 本机与外界的基本通信管理
- delphi对窗体的查询(delphi xe2)
- C#中的委托和事件 (4)---事件和委托的编译代码
- 【组队学习】【32期】动手学数据分析
- Software development Problem
- 【MATLAB】数据分析之多项式及其函数
- TIOBE 新榜单:Python 超越 Java 重回第二,Rust 崛起