前言:

真的越来越喜欢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的更多相关文章

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

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

  2. springboot学习笔记-3 整合redis&mongodb

    一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...

  3. SpringBoot学习- 5、整合Redis

    SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...

  4. 【SpringBoot】Springboot2.x整合Redis(一)

    备注: springboto整合redis依赖于spring-boot-starter-data-redis这个jar 一,项目环境和依赖 1.POM.xml配置 <parent> < ...

  5. spring boot 2.x 系列 —— spring boot 整合 redis

    文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...

  6. Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)

    前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...

  7. 实例讲解Springboot以Repository方式整合Redis

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

  8. 33. Springboot 系列 原生方式引入Redis,非RedisTemplate

     0.pom.xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis&l ...

  9. SpringBoot使用注解方式整合Redis

    1.首先导入使用Maven导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  10. springBoot系列教程03:redis的集成及使用

    1.为了高可用,先安装redis集群 参考我的另一篇文章 http://www.cnblogs.com/xiaochangwei/p/7993065.html 2.POM中引入redis <de ...

随机推荐

  1. 7.Git与项目

    Git简介 Git是目前世界上最先进的分布式版本控制系统 安装 sudo apt-get install git 安装成功后,运行如下命令 git 产生 Linus在1991年创建了开源的Linux, ...

  2. python安装mysql-python依赖包

    # 背景 新公司,对换工作了!接口自动化使用的是python的behave框架,因此需要折腾python了,而公司配的笔记本是windows的,因此要在windows下折腾python了 # 步骤 项 ...

  3. ASP.NET Forms 认证流程

    ASP.NET Forms 认证 Forms认证基础 HTTP是无状态的协议,也就是说用户的每次请求对服务器来说都是一次全新的请求,服务器不能识别这个请求是哪个用户发送的. 那服务器如何去判断一个用户 ...

  4. AJPFX平台讲述买卖、点差、单位,外汇的交易时间以及外汇交易者的参与者

    AJPFX平台讲解:买(多).卖(空).点差.单位 外汇保交易也就是通过外汇的升值和贬值来赚取利润.以EURUSD(欧元/美元)为例.假设目前价格为1.3820左右,即1欧元兑换1.3820美元.这个 ...

  5. [转]高端又易学的vbs表白程序了解一下

    第一个. 打开txt文件,复制以下代码粘贴进去(可以修改中文部分,其它代码不要动!).保存并关闭txt文件. msgbox("做我女朋友好吗?") msgbox("房产证 ...

  6. jQuery基础(2)

    一.jQuery的属性操作 jQuery的属性操作分为四部分: html标签属性操作:是对html文档中的标签属性进行读取,设置和移除操作.比如attr().removeAttr(): DOM属性操作 ...

  7. cad2015卸载/安装失败/如何彻底卸载清除干净cad2015注册表和文件的方法

    cad2015提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装cad2015失败提示cad2015安装未完成,某些产品无法安装,也有时候想重新安装cad2015 ...

  8. 多个JDK下TOMCAT运行设置

    当OS中含有多个JDK版本时,设置TOMCAT下JAVA环境变量信息的办法: 1.在setclasspath.bat或者setclasspath.sh下设置 set JAVA_HOME=d:\java ...

  9. js 标准二维数组变一维数组的方法

    问题:[[0, 1], [2, 3], [4, 5]] -> [0, 1, 2, 3, 4, 5]? 方法一 利用es5的arr.reduce(callback[, initialValue]) ...

  10. Pycharm配置autopep8:自动调整代码为PEP8风格

    关于PEP 8 PEP 8,Style Guide for Python Code,是Python官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高代码的可读性. 官网地址:http ...