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 对于 ...
随机推荐
- css3 perspective与translateZ变换
css3中的坐标系,rotateX就是绕着x轴旋转,rotateY就是绕着Y轴旋转,rotateZ就是绕着z轴旋转(也就是xy平面的旋转). perspective属性用来设置视点,在css3的模型中 ...
- Java基础系列 - 数组、二维数组、对象数组
package com.test2; public class demo2 { public static void main(String[] args) { /** * 一维数组使用 */ //数 ...
- 怎么看部分元素的js代码?
- 使用spring profile实现多环境切换
第一步: applicationContext.xml <!--环境配置开始--> <beans profile="production"> <con ...
- android: View, SurfaceView, GLSurfaceView, TextureView 区别与联系
区别与联系 View: 显示视图,内置画布,提供了图形绘制函数.触屏事件.按键事件函数等,必须在UI主线程内更新画面,速度较慢: SurfaceView: 基于view视图进行拓展的视图类,更适合2D ...
- mp4文件格式解析(转)
mp4文件格式解析 MP4文件格式带数据详解 MP4文件格式的解析,以及MP4文件的分割算法
- vue+Typescript初级入门
Typescript 在前端圈已经逐渐普及,Vue 2.5.0 改进了类型声明,使得对 TypeScript 更加友好 不过要想在项目中直接使用 TypeScript 仍然需要对项目进行一些改造 PS ...
- python mysql使用问题
(deeplearning2) userdeMBP:ageAndGender user$ python Python |Anaconda, Inc.| ( , ::) [GCC Compatible ...
- 批量删除Maven 仓库未下载成功.lastupdate 的文件
Windows: @echo off echo 开始... for /f "delims=" %%i in ('dir /b /s "./*lastUpdated&quo ...
- osg 在场景中绘制坐标轴(xyz)
//x y z font_size osg::Geode* makeCoordinate(float a_x,float a_y,float a_z,float font_size) { osg::r ...