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 对于 ...
随机推荐
- YII框架入门
一.安装 php运行环境安装包:http://www.phpstudy.net/a.php/207.html Yii安装包: https://github.com/yiisoft/yii2/relea ...
- 如何在一个function里面设置一个全局的变量?
答:解决方法是在function的开始插入一个global声明: def f() global x
- excel被保护或者锁定时候按住alt和enter可以输入换行
excel被保护或者锁定时候按住alt和enter可以输入换行
- Anriod手机抓包
1. 下载Fiddler http://fiddler2.com/ 2. 开启Fiddler的远程连接,Fiddler 主菜单 Tools -> Fiddler Options--> Co ...
- java集合类型源码解析之ArrayList
前言 作为一个老码农,不仅要谈架构.谈并发,也不能忘记最基础的语言和数据结构,因此特开辟这个系列的文章,争取每个月写1~2篇关于java基础知识的文章,以温故而知新. 如无特别之处,这个系列文章所使用 ...
- vsCode中如何根据屏幕宽度自动换行
在设置中搜索editor.wordWrap 文章来源:外星人来地球 欢迎关注,有问题一起学习欢迎留言.评论
- 【Oracle/Maven】Maven导入oracle11g 自携带jdbc驱动包ojdbc6.jar到本地库
Maven需要下载解压并添加到classpath,如果不明可以参考https://www.cnblogs.com/xiandedanteng/p/11403480.html 然后在命令行窗口执行: m ...
- github又提交不了代码了..... X_X
如下: 我们使用git提交代码过程中,突然就登录不上了 原因是 用户名被更改了:git@gitlab.0easy.com 是你的用户名 造成的原因是: 我们clone代码过程中选择了SSH的地址 解决 ...
- <JavaScript>使用onmousemove事件实现移动(拖拽)div 出现的关于offsetX的问题
出现的问题如下图所示(截屏看不出来看log) 再移动鼠标的过程中会不断的出现异常值导致拖动的div不断切换位置,回到左上角. 我以为是冒泡机制导致的所以添加了下面一段阻止冒泡,随便也阻止了默认事件,但 ...
- 阿里云RDS与ECS服务器数据库做主从
阿里云RDS与ECS服务器数据库做主从 [精] 里云RDS(数据库)基于飞天大规模分布式计算和存储能力,提供超高性价比的单机版实例,同时利用读写分离横向扩展读能力,满足网站类的业务需求.提供稳定.高性 ...