1.创建springboot项目

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn.hz</groupId>
  5. <artifactId>cn-web</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8.  
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.5.9.RELEASE</version>
  13. <relativePath />
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.tomcat.embed</groupId>
  27. <artifactId>tomcat-embed-jasper</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>javax.servlet</groupId>
  31. <artifactId>jstl</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>redis.clients</groupId>
  35. <artifactId>jedis</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.data</groupId>
  39. <artifactId>spring-data-redis</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-redis</artifactId>
  44. <version>RELEASE</version>
  45. </dependency>
  46.  
  47. </dependencies>
  48. </project>

AppRun.java

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3.  
  4. @SpringBootApplication
  5. public class AppRun {
  6.  
  7. public static void main(String[] args) {
  8. SpringApplication.run(AppRun.class, args);
  9. }
  10.  
  11. }

  RedisService.java

  1. import java.util.concurrent.TimeUnit;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.stereotype.Service;
  6.  
  7. @Service
  8. public class RedisService {
  9. @Autowired
  10. private RedisTemplate<String,String> redisTemplate;
  11.  
  12. //设置值
  13. public void set(String key, String val){
  14. redisTemplate.opsForValue().set(key,val);
  15. }
  16.  
  17. //设置值,及过期时间
  18. public void set(String key, String val, Integer seconds){
  19. redisTemplate.opsForValue().set(key,val);
  20. redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
  21. }
  22.  
  23. //过期时间
  24. public void expire(String key, Integer seconds){
  25. redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
  26. }
  27.  
  28. //获取值
  29. public String get(String key){
  30. return redisTemplate.opsForValue().get(key).toString();
  31. }
  32.  
  33. }

  TestRedis.java

  1. import redis.clients.jedis.Jedis;
  2.  
  3. public class TestRedis {
  4.  
  5. public static void main(String[] args) {
  6. // 设置连接服务器IP地址和访问端口
  7. Jedis jedis = new Jedis("192.168.187.142", 6379);
  8.  
  9. String s = jedis.get("name");
  10. System.out.println(s);
  11. }
  12.  
  13. }

  前提:关闭防火墙,关闭redis保护模式

SpringBoot整合redis简单实现的更多相关文章

  1. springboot整合redis简单示例

    一.项目架构 二.项目内容 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  2. springboot整合redis(简单整理)

    Redis安装与开启 我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/ ...

  3. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  4. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  5. SpringBoot系列十:SpringBoot整合Redis

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...

  6. springboot整合redis(注解形式)

    springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...

  7. springboot整合redis——redisTemplate的使用

    一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...

  8. SpringBoot整合Redis使用Restful风格实现CRUD功能

    前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...

  9. Springboot整合Redis入门完整篇,零基础入门教学教程

    记录一次简易集成Redis缓存 自定义Redisconfig配置 自定义序列化操作 加深印像 整合前提工具环境准备: 1.redis官网 https://redis.io/download 下载安装r ...

随机推荐

  1. 手撸Mysql原生语句--多表

    在开始之前,我们需要建立表,做建表和数据的准备的工作. 1.建表 create table department( id int, name varchar(20) ); create table e ...

  2. 部署项目到服务器 & 搭建博客网站

    搭建博客网站 作为名程序员,或者是网络编程爱好者,拥有一个自己的博客网站再好不过,本篇文章手把手教你部署自己的网站

  3. Python如何安装OpenCV库

    转载:https://blog.csdn.net/weixin_35684521/article/details/81953047 OpenCV的概念可百度,在此不再赘述.https://baike. ...

  4. DOS批处理中%cd%与%~dp0的区别详解

    转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解     Windows下批处理中%cd%和%~dp0都能用来表示当前 ...

  5. apt-get 安装软件时出现:“文件尺寸不符” 问题

    报错信息 命中:1 http://packages.deepin.com/deepin panda InRelease 命中:2 http://linux.teamviewer.com/deb sta ...

  6. linux下的echo

    echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下 ...

  7. javascript之判断数组的几种方法

    今天和小伙伴一起出去吃饭,有个小伙伴突然问我,你是前端是吧,问一下现在前端判断数组都有哪些方法,哈哈不知道是不是考我,当时没有说全,吃过饭后看了下自己以前的小笔记这里总结一下目前知道的所有对于数组的判 ...

  8. JAVA学习线路:day01面向对象(继承、抽象类)

    所有的文档和源代码都开源在GitHub: https://github.com/kun213/DailyCode上了.希望我们可以一起加油,一起学习,一起交流. day01面向对象[继承.抽象类] 今 ...

  9. rm -rf /*真的能删掉所有文件吗?

    大佬们对于小白问的问题经常直接就是一个rm -rf /*丢过去(逃,被丢了很多次,所以印象深刻),但玩了这么久的梗,当我真正想删库的时候,这条命令却然并卵(滑稽,删库跑路都跑不成). 查看了下文件属性 ...

  10. JavaScript写秒表

    1.HTML部分 <div id="div1"> <span id="hour">00</span> <span> ...