SpringBoot非官方教程 | 第九篇: springboot整合Redis
转载请标明出处:
原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot9-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 {
@Autowired
private 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);
@Test
public void contextLoads() {
}
@Autowired
RedisDao redisDao;
@Test
public 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
参考资料
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
SpringBoot非官方教程 | 第九篇: springboot整合Redis的更多相关文章
- (转) SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...
- SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
转载请标明出处: https://www.fangzhipeng.com/springboot/2017/07/11/springboot1 本文出自方志朋的博客 简介 spring boot 它的设 ...
- (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用
在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...
- SpringBoot进阶教程(二十四)整合Redis
缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓存的场合非 ...
- SpringBoot进阶教程(二十八)整合Redis事物
Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...
- SpringBoot进阶教程(二十六)整合Redis之共享Session
集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...
- SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot2-config-file/ 本文出自方志朋的博客 ...
随机推荐
- css 清除float的方法
首先我们要理解这个flaot 为什么要清除,作为小白来说直接颠覆了我之前学的内容,因为之前学的东西虽然碰到float后,脱离文档流后给兄弟元素或者父元素造成影响,但是都是通过option来定位 要么绝 ...
- 7、侧边栏:Menu
1.单个侧边栏 导航的代码在分析源码的时候已经分析过了,下面只看他的一些应用与方法. /* ---示例代码----*/ <ion-menu [content]="mycontent&q ...
- Java网络编程二--基于UDP的编程
DatagramSocket对象为基于UDP协议的Socket 构造器提供可以选择性绑定到指定端口和ip 创建完对象后调用:receive(DatagramPacket p) send(Dategra ...
- Spring-boot2.0.1.BUILD-SNAPSHOT整合Elasticsearch报failed to load elasticsearch nodes错误解决办法
spring-boot整合es的application.properties的默认配置为: spring.data.elasticsearch.cluster-nodes=localhost:9200 ...
- python函数名称空间与作用域、闭包
一.命名空间概念 1.命名空间(name space) 名称空间是存放名字的地方. 若变量x=1,1存放在内存中,命名空间是存放名字x.x与1绑定关系的地方. 2.名称空间加载顺序 python te ...
- stark——pop功能(admin中添加功能)
一.pop功能需要实现的功能和问题 1.如何在一对多和多对多字段后渲染 +2.+对应的跳转路径是什么3.保存添加记录同时,将原页面的对应的下拉菜单中添加该记录 二.window.open()方法详解 ...
- SQLAlchemy的使用---数据库的创建与连接
# 1. 导入SQLAlchemy from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Col ...
- canvas的getImageData和putImageDataAPI
- SpannableString与SpannableStringBuilder使用
转自:http://blog.it985.com/14433.html1.SpannableString.SpannableStringBuilder与String的关系 首先SpannableStr ...
- Qt之QSS(Q_PROPERTY-原始属性)
http://blog.csdn.net/liang19890820/article/details/51698536 版权声明:进步始于交流,收获源于分享!纯正开源之美,有趣.好玩.靠谱...作者: ...