Spring Boot 知识笔记(整合Redis)
一、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、可以使用默认的配置,如有需要可以在application.properties中增加
三、新建操作redis的controller
package net.Eleven.base_project.controller; import net.Eleven.base_project.domain.JsonData; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api/redis")
public class RdisTestController { @Autowired
private StringRedisTemplate redisTpl; //注入操作redis的模板 @GetMapping(value="add")
public Object add(){
redisTpl.opsForValue().set("name", "Eleven");//通过set向redis里面添加数据
return JsonData.buildSuccess();
} @GetMapping(value="get")
public Object get(){
String value = redisTpl.opsForValue().get("name");
return JsonData.buildSuccess(value); //通过get取数据
} }
四、利用JsonData返回结果
package net.Eleven.base_project.domain; import java.io.Serializable;
public class JsonData implements Serializable { private static final long serialVersionUID = 1L; private Integer code; // 状态码 0 表示成功,1表示处理中,-1表示失败
private Object data; // 数据
private String msg;// 描述 public JsonData() {
} public JsonData(Integer code, Object data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
} // 成功,传入数据
public static JsonData buildSuccess() {
return new JsonData(0, null, null);
} // 成功,传入数据
public static JsonData buildSuccess(Object data) {
return new JsonData(0, data, null);
} // 失败,传入描述信息
public static JsonData buildError(String msg) {
return new JsonData(-1, null, msg);
} // 失败,传入描述信息,状态码
public static JsonData buildError(String msg, Integer code) {
return new JsonData(code, null, msg);
} // 成功,传入数据,及描述信息
public static JsonData buildSuccess(Object data, String msg) {
return new JsonData(0, data, msg);
} // 成功,传入数据,及状态码
public static JsonData buildSuccess(Object data, int code) {
return new JsonData(code, data, null);
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} @Override
public String toString() {
return "JsonData [code=" + code + ", data=" + data + ", msg=" + msg + "]";
} }
五、http://127.0.0.1:8080/api/redis/add

六、http://127.0.0.1:8080/api/redis/get

Spring Boot 知识笔记(整合Redis)的更多相关文章
- Spring Boot 学习笔记--整合Redis
1.新建Spring Boot项目 添加spring-boot-starter-data-redis依赖 <dependency> <groupId>org.springfra ...
- Spring Boot 2.x 整合 Redis最佳实践
一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...
- Spring Boot 2.x整合Redis
最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...
- Spring Boot WebFlux-06——WebFlux 整合 Redis
第06课:WebFlux 整合 Redis 前言 上一篇内容讲了如何整合 MongoDB,这里继续讲如何操作 Redis 这个数据源,那什么是 Reids? Redis 是一个高性能的 key-val ...
- Spring Boot 知识笔记(整合Mybatis)
一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...
- spring boot 自学笔记(四) Redis集成—Jedis
上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例. 修改Red ...
- Spring Boot不同版本整合Redis的配置
1. Spring Boot为1.4及其他低版本 1.1 POM.XML配置 <!--引入 spring-boot-starter-redis(1.4版本前)--> <depende ...
- Spring Boot 学习笔记--整合Thymeleaf
1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...
- Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档
1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...
- Spring Boot 知识笔记(配置文件)
Spring boot 提供了两种常用的配置文件,properties和yml文件. 1.yml yml是YAML(YAML Ain't Markup Language)语言的文件,以数据为中心,比j ...
随机推荐
- Django中创建对象的组合
一.问题背景 在Django中一个表可能是多个表共同合成的对象,比如商品表,用户表,用户购买商品的表,就是这种情况,在这中情况下面我们要添加一条记录到用户购买的商品表中我们该如何才做,此时我们需要获取 ...
- DevExpress的图形按钮菜单栏控件WindowsUIButtonPanel的布局、使用和设置按钮的点击事件
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- Win10 手动安装 WSL 并修改默认登录用户为 root
首先要在"程序和功能"里面开启这个服务 然后重启系统使其生效. 然后打开 PowerShell,输入: Invoke-WebRequest -Uri https://aka.ms/ ...
- React入门(二)
组件的生命周期 概念:在组件创建.到加载到页面运行.以及组件被销毁的过程中伴随的事件.组件的生命周期是指在组件的特定时期触发的事件. 组件的生命周期分为三个部分: 组件创建阶段:只执行一次 compo ...
- flink 实现三角枚举EnumTriangles算法详解
1.三角枚举,从所有无向边对中找到相互连接的三角形 /** * @Author: xu.dm * @Date: 2019/7/4 21:31 * @Description: 三角枚举算法 * 三角枚举 ...
- JavaScript深入浅出第5课:Chrome是如何成功的?
摘要: Chrome改变世界. <JavaScript深入浅出>系列: JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼? JavaScript深入浅出第2课:函数是一 ...
- 【原创】CentOS 7 安装airflow
该文是基于python虚拟化环境来安装,非虚拟化也是一样,虚拟化我只是不想破环系统环境. 安装python虚拟环境 pip install virtualenv 设置环境变量 sudo vi /etc ...
- 模块 time,datetime,random,typing,hashlib,requests,re
目录 包 什么是包 为什么要包 1. 包的介绍 2. 绝对导入和相对导入 - 绝对导入 - 相对导入 time模块 时间戳 格式化时间 结构化时间 sleep datetime模块 random模块 ...
- lua脚本之钉钉免密登录
nginx.conf worker_processes ; error_log logs/error.log; events { worker_connections ; } http { resol ...
- linux 进程通信之 信号
一,管道PIPE 二,FIFO通信 三,mmap通信 四,信号的概念 信号的特点:简单,但不能携带大量的信息,满足特定条件就会发生 信号的机制:进程B发送信号给进程A.信号是由内核来处理的. 信号的产 ...