GitHub:https://github.com/asd821300801/Spring-Boot/tree/spring-boot-redis


前期准备

  • 创建Spring Boot 工程
......

  • Maven 加入必要的依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>1.5.7.RELEASE</version>
</dependency>

  • application.properties
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.database=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0

  • 启动redis


连接redis做相应的数据操作

查看源码可知 redisTemplateStringRedisTemplate已被自动配置,所以我们直接用就可以

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.class


RedisDao.java

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.Repository;@Repository
public class RedisDao {@Autowiredprivate StringRedisTemplate template;public  void setKey(String key,String value){ValueOperations<String, String> ops = template.opsForValue();ops.set(key,value);}public String getValue(String key){ValueOperations<String, String> ops = this.template.opsForValue();return ops.get(key);}
}

RedisController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.dao.RedisDao;@RestController
public class RedisController {@Autowiredprivate RedisDao redisDao;@RequestMapping("/set")public String set(String key,String value){redisDao.setKey(key, value);return "success";}@RequestMapping("/get")public String get(String key){return redisDao.getValue(key);}
}

访问测试

设置数据:http://localhost:8080/set?key=lingdu&value=123456


获取数据:http://localhost:8080/get?key=lingdu

spring-boot-starter-data-redis相关推荐

  1. 一个项目有两个pom_实现一个Spring Boot Starter超简单,读 Starter 源码也不在话下...

    Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...

  2. Spring Boot中使用Redis数据库

    Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, So ...

  3. 《SpringCloud超级入门》Spring Boot Starter的介绍及使用《七》

    目录 Spring Boot Starter项目创建 自动创建客户端 使用 Starter 使用注解开启 Starter 自动构建 使用配置开启 Starter 自动构建 配置 Starter 内容提 ...

  4. 实现一个 Spring Boot Starter 原来如此简单,读 Starter 源码也不在话下

    我是风筝,公众号「古时的风筝」,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者.现已转行程序员鼓励师 Spring Cloud 系列文章已经完成,可以 ...

  5. 手把手教你定制标准 Spring Boot starter

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 写在前面 我们每次构建一个 Spring 应用程序时,我 ...

  6. 自定义 Spring Boot Starter

    一.引言 什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧. Starters are a set of convenient dependency descripto ...

  7. Spring boot starter

    1:Spring boot starter及项目中的类似运用 1:Spring boot starter的两种方式 引入pom文件,自动管理jar版本 根据spring.factories配置文件,加 ...

  8. Spring Boot之基于Redis实现MyBatis查询缓存解决方案

    转载自 Spring Boot之基于Redis实现MyBatis查询缓存解决方案 1. 前言 MyBatis是Java中常用的数据层ORM框架,笔者目前在实际的开发中,也在使用MyBatis.本文主要 ...

  9. 一个简易上手的短信服务Spring Boot Starter,连傻瓜都会!

    作 者:jackieonway 来 源:jianshu.com/u/36510c75d37c 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式 ...

  10. 快速开发一个自定义 Spring Boot Starter ,希望你也会

    来源:http://t.cn/Ai9li9fC 众所周知,Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增.在传统Maven项目中通常将一些层.组件拆分为 ...

最新文章

  1. 不一样的命令行 – Windows PowerShell简介
  2. 天气情况(思维,dp思想)
  3. Android在Context详细解释 ---- 你不知道Context
  4. 打开本地文件_可以跨软件搜索文件?结构式检索你一定要学会!
  5. gradle groovy_适用于Java开发人员的Groovy吗? 认识Gradle,Grails和Spock
  6. 很认真地聊一聊程序员的自我修养
  7. 《Python Cookbook 3rd》笔记(3.3):数字的格式化输出
  8. python 线程通信 会涉及到拷贝吗_Python如何实现线程间通信
  9. Fedora上配置一个安全FTP
  10. 天正电气lisp是什么文件_模仿天正电气低压系统图 - AutoLISP/Visual LISP 编程技术 - CAD论坛 - 明经CAD社区 - Powered by Discuz!...
  11. 计算机辅助几何设计知识点,计算机辅助几何设计
  12. 期末作业面向对象程序设计(Java)“猜拳游戏”
  13. n一加关闭小部件_小部件
  14. java sam接口,Java中的SAM接口是什么?
  15. widows 句柄HINSTANCE
  16. Vue_(基础)Vue中的事件
  17. 概率 (菜鸡 dalao轻喷
  18. JMeter教程1 - 介绍环境搭建 (Mac)插件安装
  19. QT:表格操作QTableView详解
  20. 深入浅出Java23种设计模式(一)

热门文章

  1. 买玉时怎么鉴别是不是好玉?
  2. 微信小程序 api+前端实现生成分享海报
  3. 风、寒、暑、湿、燥、火
  4. BuddyPress汉化中文语言包 BuddyPress汉化教程步骤
  5. Kaggle入门和学习资源
  6. android:layout_marginright 不起作用,为什么RelativeLayout的marginBottom和marginRight无法使用...
  7. 蘑菇街2016研发工程师编程题--回文串
  8. c语言蚂蚁搬,小蚂蚁搬豆阅读答案
  9. 全国计算机竞赛能保送清华北大吗,NOIP考试是什么?能保送清华北大是真的吗?...
  10. 手把手教你从头开始搭建友善之臂ARM-tiny4412开发环境(史上最详细!!)