前言:

真的越来越喜欢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. 论EFMS模拟量部分采集电路的修改

    论1:电阻R11的作用 如图1是2014-3-11之前模拟量采集的部分硬件电路,图2是纠正后的正确电路. D5是SA20CA,TVS双向二极管,有效防止外接电源的浪涌冲击情况,保护电路.  D6是稳压 ...

  2. 4.动态HTML处理和机器图像识别

    Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可以直接运行在浏览器上, ...

  3. 64位进程调用32位dll的解决方法

    64位进程调用32位dll的解决方法   最近做在Windows XP X64,VS2005环境下做32位程序编译为64位程序的工作,遇到了一些64位编程中可能遇到的问题:如内联汇编(解决方法改为C/ ...

  4. Android-Java-synchronized同步代码块的使用场景

    synchronized同步代码块的使用场景 (满足以下两种条件,就要考虑使用synchronize同步代码块了) 1.被synchronized同步代码块{同步的代码},是被多次异步调用,什么叫多次 ...

  5. [leet code 165]Compare Version Numbers

    1 题目 Compare two version numbers version1 and version2.If version1 > version2 return 1, if versio ...

  6. linux 下查看redis内存等占用量

    用客户端连接redis服务器:  ./redis-cli -h ip :port >>info server : 一般 Redis 服务器信息,包含以下域: redis_version : ...

  7. Tree with Small Distances(cf1029E)(树形动规)

    You are given an undirected tree consisting of \(n\) vertices. An undirected tree is a connected und ...

  8. 程序猿的日常——Mybatis现学现卖

    最近有一个小项目需求,需要用spring mvc + mybatis实现一个复杂的配置系统.其中遇到了很多不太常见的问题,在这里特意记录下: 主要涉及的内容有 事务 多表删除 插入并返回主键 1 sp ...

  9. lucene使用与优化

    lucene使用与优化 1 lucene简介 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿 ...

  10. postgresql-查看表大小

    drop table tablesize create table tablesize( phone int) create table tablesize( phone text) create t ...