【SpringBoot系列2】SpringBoot整合Redis
前言:
真的越来越喜欢SpringBoot了,这是SpringBoot学习系列之一。
正文:
1:首先在pom文件中添加依赖,记得是spring-boot-starter-data-redis,不是spring-boot-starter-redis
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2:第二步在properties文件中配置数据源
# redis
spring.redis.host=120.78.159.xxx
spring.redis.port=xxxx
spring.redis.password=xxxx
3: 第三步,直接注入即可
@Autowired
StringRedisTemplate redis;
是不是很爽,我用的时候被惊呆了,怎么可以这么简单,这么简洁,太感觉开源社区的大神发明了springboot这个框架。
附上官方的API:https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/StringRedisTemplate.html
用法:
1:测试是否连接redis成功,并且取一些key值出来
// 测试redis
@RequestMapping(value = "/testRedis", method = RequestMethod.GET)
public String testRedis() { List<String> list = new ArrayList<>();
list.add("k1");
list.add("k2");
list.add("k3");
System.out.println(redis.opsForValue().multiGet(list)); return redis.opsForValue().multiGet(list).toString();
}
2:测试set数据进redis,并且设置超时机制
// put key
@RequestMapping(value = "/putRedis/{id}", method = RequestMethod.GET)
public String putRedis(@PathVariable(value = "id") String id) { String key = "Test:" + id;
String value = "I Love zxx";
redis.opsForValue().set(key, value);
redis.expire(key, Integer.MAX_VALUE, TimeUnit.SECONDS); return "put key to Redis";
}
3: 测试从redis里面get数据出来
// get key
@RequestMapping(value = "/getRedis/{id}", method = RequestMethod.GET)
public String getRedis(@PathVariable(value = "id") String id) { String key = "Test:" + id;
System.out.println(redis.opsForValue().get(key)); return "get key from redis";
}
【SpringBoot系列2】SpringBoot整合Redis的更多相关文章
- 实例讲解Springboot以Template方式整合Redis及序列化问题
1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...
- springboot学习笔记-3 整合redis&mongodb
一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...
- SpringBoot学习- 5、整合Redis
SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...
- 【SpringBoot】Springboot2.x整合Redis(一)
备注: springboto整合redis依赖于spring-boot-starter-data-redis这个jar 一,项目环境和依赖 1.POM.xml配置 <parent> < ...
- spring boot 2.x 系列 —— spring boot 整合 redis
文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...
- 实例讲解Springboot以Repository方式整合Redis
1 简介 Redis是高性能的NoSQL数据库,经常作为缓存流行于各大互联网架构中.本文将介绍如何在Springboot中整合Spring Data Redis,使用Repository的方式操作. ...
- 33. Springboot 系列 原生方式引入Redis,非RedisTemplate
0.pom.xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis&l ...
- SpringBoot使用注解方式整合Redis
1.首先导入使用Maven导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- springBoot系列教程03:redis的集成及使用
1.为了高可用,先安装redis集群 参考我的另一篇文章 http://www.cnblogs.com/xiaochangwei/p/7993065.html 2.POM中引入redis <de ...
随机推荐
- file新建文件及文件夹
1.获取包名的根目录:mRootPath = getFilesDir().getParent(); // ====mRootPath===/data/data/com.yoyu.file 获取SD卡 ...
- Android学习之可滑动当前的Activity视图看见上一个活动的视图
先看一下我的效果图吧: 好大的图啊!!! 百度音乐由一个很酷的功能,当前的UI可以滑动,然后看见上一个活动的UI,当时顿时觉得百度的牛人好多啊,能将如此前沿的技术应用到app上.当然如果你熟悉了And ...
- ASP.NET Web API 入门 (API接口、寄宿方式、HttpClient调用)
一.ASP.NET Web API接口定义 ASP.NET Web API默认实现了Action方法和HTTP方法的映射,Action方法方法名体现了其能处理的请求必须采用的HTTP方法 二.寄宿方式 ...
- oracle 中top-n的使用
对于ms sqlserver数据库中可以直接使用top(n)提取前N 个结果,而oracle中并不能直接使用的.oracle中提供了对于提取前N 条的结果的方法 那就是用行编号 例如:select ...
- zeppelin中运行spark streaming kakfa & 实时可视化
notebook方式运行spark程序是一种比较agile的方式,一方面可以体验像spark-shell那样repl的便捷,同时可以借助notebook的作图能力实现快速数据可视化,非常方便快速验证和 ...
- MySQL--Percona-XtraDB-Cluster 5.6安装笔记
安装环境: 有三台干净的CentOS 6的服务器,IP配置为:192.168.166.169,192.168.166.170,192.168.166.171,准备搭建三节点的Percona XtraD ...
- MVC所有的ActionResult
一.所有的Controller都继承自System.Web.Mvc.Controller 目前ASP.NET MVC3默认提供了多种ActionResult的实现,在System.Web.Mvc命名空 ...
- Swift5 语言参考(三) 类型
在Swift中,有两种类型:命名类型和复合类型.一个名为类型是当它的定义可以给出一个特定名称的类型.命名类型包括类,结构,枚举和协议.例如,名为的用户定义类的实例MyClass具有该类型MyClass ...
- cisco 的ACL
搞网络好几年了,怎么说呢,水平一直停留在NA-NP之间,系统的学完NA后,做了不少实验,后来也维护了企业的网络,各种网络设备都玩过(在商汤用的Juniper srx 550 我认为在企业环境,非IDC ...
- sql 游标 跳出循环 和进入下一个循环
1 使用break 结束整个循环. 2 使用continue 结束当前循环,进入下已循环. 注意:使用continue造成死循环,是因为continue后又执行与上次相同的fetch了. 解决办法 ...