一.创建项目

项目名称为 “springboot_redis”,创建过程中勾选 “Web”,“Redis”,第一次创建Maven需要下载依赖包(耐心等待)

二.实现

properties配置文件中添加配置信息

##########redis############

#redis的IP地址
spring.redis.host=localhost
#redis的端口
spring.redis.port=6379
#redis的密码
spring.redis.password=123456
#redis默认有16个数据库,使用DB0
spring.redis.database=0

创建RedisComponent类

package com.woniu.RedisComponent;

import org.apache.hadoop.mapred.gethistory_jsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component; @Component
public class RedisComponent {
@Autowired
private StringRedisTemplate stringRedisTemplate; public void set(String key, String value){
ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
boolean bExistent = this.stringRedisTemplate.hasKey(key);
if (bExistent) {
System.out.println("this key is bExistent!");
}else{
ops.set(key, value);
}
} public String get(String key){
return this.stringRedisTemplate.opsForValue().get(key);
} public void del(String key){
this.stringRedisTemplate.delete(key);
}
}

创建WebController类

package com.woniu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.woniu.RedisComponent.RedisComponent; @RestController
@RequestMapping(value="/web")
public class WebController { @Autowired
private RedisComponent redisComponet; @RequestMapping(value="/set/{key}/{value}")
public String set(@PathVariable String key, @PathVariable String value){
redisComponet.set(key, value);
return "set key succ!";
} @RequestMapping(value="/get/{key}")
public String get(@PathVariable String key){
return redisComponet.get(key);
} @RequestMapping(value="/del/{key}")
public void del(@PathVariable String key){
redisComponet.del(key);
}
}

本机安装redis,设置密码为123456,启动redis。

测试:

工程springboot_redis源码下载地址:点击打开链接

spring boot讨论群:611262656,一键加群:点击加群

更多技术文章请关注微信公众号“Java架构师之路”:


Spring Boot系列教程十二:Spring boot集成Redis的更多相关文章

  1. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置

    用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...

  2. Spring Boot系列教程十:Spring boot集成MyBatis

    一.创建项目         项目名称为 "springboot_mybatis_demo",创建过程中勾选 "Web","MyBatis" ...

  3. Spring Boot系列教程十:Spring boot集成Sentinel Redis

    前言 上一篇文章介绍了spring boot集成单点的redis,然而实际生产环境使用单点的redis风险很高,一旦宕机整个服务将无法使用,这篇文章介绍如何使用基于sentinel的redis高可用方 ...

  4. Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate

    在 Java 领域,数据持久化有几个常见的方案,有 Spring 自带的 JdbcTemplate .有 MyBatis,还有 JPA,在这些方案中,最简单的就是 Spring 自带的 JdbcTem ...

  5. Spring Boot2 系列教程 (十二) | 整合 thymeleaf

    前言 如题,今天介绍 Thymeleaf ,并整合 Thymeleaf 开发一个简陋版的学生信息管理系统. SpringBoot 提供了大量模板引擎,包含 Freemarker.Groovy.Thym ...

  6. Spring Boot2 系列教程(十二)@ControllerAdvice 的三种使用场景

    严格来说,本文并不算是 Spring Boot 中的知识点,但是很多学过 SpringMVC 的小伙伴,对于 @ControllerAdvice 却并不熟悉,Spring Boot 和 SpringM ...

  7. CRL快速开发框架系列教程十二(MongoDB支持)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  8. webpack4 系列教程(十二):处理第三方JavaScript库

    教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十二):处理第三方 JavaScript 库>原文地址.或者来我的小站看更多内容:godbm ...

  9. Spring Boot2 系列教程(三十一)Spring Boot 构建 RESTful 风格应用

    RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...

随机推荐

  1. CF1174D Ehab and the Expected XOR Problem(二进制)

    做法 求出答案序列的异或前缀和\(sum_i\),\([l,r]\)子段异或和可表示为\(sum_r\bigoplus sum_{l-1}\) 故转换问题为,填\(sum\)数组,数组内的元素不为\( ...

  2. 使用 Nexus3 Repository Manager 搭建 npm 私服

    公司里一般都有自己的私服,用于管理封装的工具插件等,Nexus2主要是用于maven/gralde仓库的统一管理,Nexus3则添加了npm插件,可以对npm提供支持,其实用于npm仓库管理的还有一个 ...

  3. 个人学习分布式专题(二)分布式服务治理之分布式协调技术Zookeeper

    分布式协调技术Zookeeper 2.1 zookeeper集群安装部署(略) 2.2 zookeeper的基本原理,数据模型 2.3 zookeeper Java api的使用 2.4 zookee ...

  4. [RK3399] 修改移动网络默认为4G

    CPU:RK3399 系统:Android 7.1 现在手机卡都默认是 4G 网路,但是源码中默认的还是 3G网络,每次都要手动改到 4G. 下面在源码中就直接将默认网络改为4G. PREFERRED ...

  5. OpenResty之 limit.count 模块

    原文: lua-resty-limit-traffic/lib/resty/limit/count.md 1. 示例 http { lua_shared_dict my_limit_count_sto ...

  6. CTR预估之LR与GBDT融合

    转载自:http://www.cbdio.com/BigData/2015-08/27/content_3750170.htm 1.背景 CTR预估,广告点击率(Click-Through Rate ...

  7. arcgis python 开启编辑会话和编辑操作、在表中创建行、停止编辑操作以及提交编辑会话。

    import arcpy import os fc = 'Database Connections/Portland.sde/portland.jgp.schools' workspace = os. ...

  8. Flume-Hbase-Sink针对不同版本flume与HBase的适配研究与经验总结

    https://cloud.tencent.com/developer/article/1025430 Flume-Hbase-Sink针对不同版本flume与HBase的适配研究与经验总结 mike ...

  9. JSP页面中如何注入Spring容器中的bean

    第一步在JSP页面中导入下面的包: <%@page import="org.springframework.web.context.support.WebApplicationCont ...

  10. jenkins参数化构建&HTML报告

    背景: 每次构建项目时都需要去修改一下配置,然后保存,再去立即构建.这样修改容易修改出错误,影响到执行脚本,且每次都要去修改配置,不容易修改,操作也比较麻烦.所以决定将Jenkins修改为参数化构建. ...