前言:

真的越来越喜欢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. hive中数据存储格式对比:textfile,parquent,orc,thrift,avro,protubuf

    这篇文章我会从业务中关注的: 1. 存储大小 2.查询效率 3.是否支持表结构变更既数据版本变迁 5.能否避免分隔符问题 6.优势和劣势总结 几方面完整的介绍下hive中数据以下几种数据格式:text ...

  2. bootstrap table 前端搜索

    1.bootstrap-table对于前端的搜索可以通过官网设置,但发现前端搜索出现bug,网上找到一个bootstrap-table的扩充js  bootstrap-table-mytoolbar. ...

  3. 由异常掉电问题---谈xfs文件系统

    由异常掉电问题---谈xfs文件系统 本文皆是作者自己的学习总结或感悟(linux环境),如有不对,欢迎提出一起探讨!! 目录结构 一.相关知识 二.问题提出 三.处理方法 四.最终结果 一.相关知识 ...

  4. C# .NET 获取枚举值的自定义属性

    一.定义一个类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  5. CentOS 7.0 Firewall防火墙配置

    启动停止 获取firewall状态 systemctl status firewalld.service firewall-cmd --state 开启停止防火墙 开机启动:systemctl ena ...

  6. C#调用haskell遭遇Attempted to read or write protected memory

    1. Haskell的代码如下: 上面的代码中readMarkdown与writeHtmlString是pandoc中的函数,newString的作用是将String转换为IO CString. 2. ...

  7. JS学习笔记3_函数表达式

    1.函数表达式与函数声明的区别 函数声明有“提升”(hoisting)的特性,而函数表达式没有.也就是说,函数声明会在加载代码时被预先加载到context中,而函数表达式只有在执行表达式语句时才会被加 ...

  8. 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊

    BZOJ2002 [Hnoi2010]Bounce 弹飞绵羊 Solution 很早以前写的一道分块题,最近在搞LCT,又做了一遍. 1.LCT做法 看到这种动态修改,想下LCT怎么维护. 修改操作就 ...

  9. Linux之IRQ domain

    概述 Linux使用IRQ domain来描述一个中断控制器(IRQ Controller)所管理的中断源.换句话说,每个中断控制器都有自己的domain.我们可以将IRQ Domain看作是IRQ ...

  10. postgresql-pgbench(转)

    pgbench测试:   pg9.6.2的pgbench报错: [thunisoft@localhost ~]$ pgbench -S -c 8 -t 60 pgbenchdb Segmentatio ...