Spring Boot系列教程十二:Spring boot集成Redis
一.创建项目
项目名称为 “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的更多相关文章
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
- Spring Boot系列教程十:Spring boot集成MyBatis
一.创建项目 项目名称为 "springboot_mybatis_demo",创建过程中勾选 "Web","MyBatis" ...
- Spring Boot系列教程十:Spring boot集成Sentinel Redis
前言 上一篇文章介绍了spring boot集成单点的redis,然而实际生产环境使用单点的redis风险很高,一旦宕机整个服务将无法使用,这篇文章介绍如何使用基于sentinel的redis高可用方 ...
- Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
在 Java 领域,数据持久化有几个常见的方案,有 Spring 自带的 JdbcTemplate .有 MyBatis,还有 JPA,在这些方案中,最简单的就是 Spring 自带的 JdbcTem ...
- Spring Boot2 系列教程 (十二) | 整合 thymeleaf
前言 如题,今天介绍 Thymeleaf ,并整合 Thymeleaf 开发一个简陋版的学生信息管理系统. SpringBoot 提供了大量模板引擎,包含 Freemarker.Groovy.Thym ...
- Spring Boot2 系列教程(十二)@ControllerAdvice 的三种使用场景
严格来说,本文并不算是 Spring Boot 中的知识点,但是很多学过 SpringMVC 的小伙伴,对于 @ControllerAdvice 却并不熟悉,Spring Boot 和 SpringM ...
- CRL快速开发框架系列教程十二(MongoDB支持)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- webpack4 系列教程(十二):处理第三方JavaScript库
教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十二):处理第三方 JavaScript 库>原文地址.或者来我的小站看更多内容:godbm ...
- Spring Boot2 系列教程(三十一)Spring Boot 构建 RESTful 风格应用
RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...
随机推荐
- python3 pyinstaller 图标改变不了的问题
命令 pyinstaller -F ./test.py --noconsole --icon=test.ico 在使用后可能发现新生成的图标仍然为默认图标,有以下解决方案: 将生成的exe文件复制到另 ...
- 一些scala的操作
Scala获取当前目录下所有文件 import java.io.File //获取目录下的所有文件,当前项目目录输入new File(".") def getFiles1(dir: ...
- chtMultiRegionSimpleFoam求解器的热源不在边界上【翻译】
翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-solving/126777-chtmultiregionsimplefoa ...
- Leetcode题 112 和 113. Path Sum I and II
112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- httpPostedFile实现WEBAPI文件上传
public void PostUpload() { var httpPostedFile = HttpContext.Current.Request.Files; foreach(string p ...
- arcgis python 使用光标和内存中的要素类将数据加载到要素集 学习:http://zhihu.esrichina.com.cn/article/634
学习:http://zhihu.esrichina.com.cn/article/634使用光标和内存中的要素类将数据加载到要素集 import arcpy arcpy.env.overwriteOu ...
- 微信小程序:自定义导航栏
在小程序开发的时候会遇到一些页面不需要头部导航,或像淘宝的商品详情一样自定义的导航栏.那先要清楚这导航可不能写死,每种手机的导航都各不相同. 一.在app.json的window对象中定义导航的样式: ...
- matlab图像灰度调整——imadjust函数的使用
在MATLAB中,通过函数imadjust()进行图像灰度的调整,该函数调用格式如下: J=imadjust( I ) 对图像I进行灰度调整 J=imadjust( I,[low_in;high_i ...
- 两个ESP8266一个作为服务器一个作为客户端实现互相通讯
两个ESP8266一个作为服务器一个作为客户端实现互相通讯
- LC 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...