SpringBoot学习足迹

SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool

1、pom.xml添加dependency

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>

2、下面封装一个工具类,utils新建JedisPoolUtil,先只封装读写字符串

package com.jgui.utils;

import com.jgui.config.RedisConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; /**
* @Author: zhaogaojian
* @Description:
* @Date: Created in 2020/1/821:54
*/
@Component
public class JedisPoolUtil {
private JedisPool jedisPool=null;
@Autowired
private RedisConfig redisConfig;
private JedisPool getJedisPool() {
if(jedisPool==null)
{
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait());
jedisPool=new JedisPool(jedisPoolConfig, redisConfig.getHost(),redisConfig.getPort(),redisConfig.getTimeout()*1000,redisConfig.getPassword(),0);
}
return jedisPool;
}
public String set(String key,String value, int cacheSeconds)
{
String strRet = null;
Jedis jedis =null;
try {
jedis = getJedisPool().getResource();
strRet=jedis.set(key,value);
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
//logger.error(e.getMessage(), e);
} finally {
if (jedis != null)
jedis.close(); //注意这里不是关闭连接,在JedisPool模式下,Jedis会被归还给资源池。
}
return strRet;
}
public String get(String key)
{
String strRet=null;
Jedis jedis =null;
try {
jedis = getJedisPool().getResource();
strRet=jedis.get(key);
} catch (Exception e) {
//logger.error(e.getMessage(), e);
} finally {
if (jedis != null)
jedis.close(); //注意这里不是关闭连接,在JedisPool模式下,Jedis会被归还给资源池。
}
return strRet;
}
}

3、Config文件新建RedisConfig

package com.jgui.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component; /**
* @Author: zhaogaojian
* @Description:
* @Date: Created in 2020/1/822:51
*/
@Data
@Component
@ConfigurationProperties(prefix = "redis") //把同类的配置信息自动封装成实体类
public class RedisConfig {
private String host="127.0.0.1";
private Integer port=6379;
private Integer timeout=3; //秒
private String password="123456"; private Integer poolMaxTotal=10;
private Integer poolMaxIdle=10;
private Integer poolMaxWait=3;//秒
}

4、application.properties增加配置节

#--------------------Jedis配置-------------------
redis.host=127.0.0.1
redis.port=6379
#秒
redis.timeout=5
redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
#秒
redis.poolMaxWait=3

5、修改hello进行测试

    @Autowired
private JedisPoolUtil jedisPoolUtil;
@RequestMapping("/hello")
public String hello() { String ret=jedisPoolUtil.set("111","123",60);
String ret1=jedisPoolUtil.get("111");
return ret1;
//return userDao.selectByPrimaryKey(1).getRealname();
//return "Hello World11";
}

6、拦截器增加hello排除

public void addInterceptors(InterceptorRegistry registry){
List<String> excludePath = new ArrayList<>();
excludePath.add("/login"); //登录
excludePath.add("/hello"); //测试
registry.addInterceptor(tokenInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(excludePath);
WebMvcConfigurer.super.addInterceptors(registry); }

7、运行测试,返回123,工作正常

以上参考

https://www.jianshu.com/p/df57fefe0ab7

https://www.jdon.com/51938

https://blog.csdn.net/MRA__S__/article/details/82051538

https://blog.csdn.net/damanchen/article/details/103770222

SpringBoot学习- 5、整合Redis的更多相关文章

  1. SpringBoot学习:整合Redis

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 pom.xml添加对redis的依赖: <!-- https://mvnrepos ...

  2. SpringBoot学习笔记:Redis缓存

    SpringBoot学习笔记:Redis缓存 关于Redis Redis是一个使用ANSI C语言编写的免费开源.支持网络.可基于内存亦可以持久化的日志型.键值数据库.其支持多种存储类型,包括Stri ...

  3. SpringBoot学习之整合Mybatis

    本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...

  4. 【快学springboot】11.整合redis实现session共享

    前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...

  5. SpringBoot学习之整合Druid的简单应用

    一.Druid介绍 Druid简介 Druid是目前Java语言中最好的数据库连接池之一.结合了 C3P0.DBCP 等 DB 池的优点,同时加入了日志监控.Druid 是一个分布式的.支持实时多维 ...

  6. SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制

    一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...

  7. SpringBoot学习之整合Swagger

    Swagger介绍 1.什么是Swagger 作为后端程序开发,我们多多少少写过几个后台接口项目,不管是编写手机端接口,还是目前比较火热的前后端分离项目,前端与后端都是由不同的工程师进行开发,那么这之 ...

  8. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

    平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...

  9. 25、springboot与缓存整合Redis

    默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中 docker: 安装Redis: 查看官方文档: 添加约束 <dependency> <gro ...

  10. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...

随机推荐

  1. kuangbin专题 专题九 连通图 Warm up HDU - 4612

    题目链接:https://vjudge.net/problem/HDU-4612 题目:一个大地图,给定若干个连通图,每个连通图中有若干个桥,你可以在任意某个连通图的 任意两个点添加一条边,问,添加一 ...

  2. Html介绍,标签的语法

    1.标签由英文"<"和">"括起来组成,如<html>就是一个标签2.html中的标签一般都是成对成对出现的,分为开始标签和结束标签.结 ...

  3. shell 一键配置单实例oracle基础环境变量(linux7)

    #!/bin/bash echo "修改主机名" hostnamectl set-hostname wangxfa hostname sleep 1 echo "查看并关 ...

  4. 如何修改Tomcat运行时jvm编码

    问题: 最近在部署项目的时候出现数据乱码的情况,经过一番查看项目都是用的UTF-8编码格式,数据也是,但是经过调用接口传给对方就乱码了. 由于是部署在Windows环境下,Windows默认编码GBK ...

  5. MySQL 8 InnoDB 集群生产部署

    生产部署InnoDB集群 1.先决条件 InnoDB集群使用组复制技术,所以InnoDB中的实例需要满足组复制要求.可以查看MySQL文档中组复制相关的部分,也可以通过AdminAPI提供的dba.c ...

  6. JQuery调用WebService封装方法

    //提交的webservice链接 //var url = "/wsstafffrate?OpenWebService"; //请求前拼接好的soap字符串 //var soapd ...

  7. cf1037E

    题解:考虑逆序处理询问,用一个set来维护能去的人,每次减少边的时候,维护一下这个set就可以,具体看代码 int main(){ int n, m, k; cin >> n >&g ...

  8. Linux——基础之vi编辑器,编辑器之神!

    VI编辑器是什么? 我们学了怎么多的命令,都是为了我们的linux系统和远程操作的方便,那么我们现在怎么,编辑服务器上的文件和软件呢? 换句话说,就是我们如何通过命令行去完成文本和代码的编写,和系统的 ...

  9. 论文阅读笔记(十七)【ICCV2017】:Dynamic Label Graph Matching for Unsupervised Video Re-Identification

    Introduction 文章主要提出了 Dynamic Graph Matching(DGM)方法,以非监督的方式对多个相机的行人视频中识别出正确匹配.错误匹配的结果.本文主要思想如下图: 具体而言 ...

  10. C#最基本的Socket编程

    示例程序是同步套接字程序,功能很简单,只是客户端发给服务器一条信息,服务器向客户端返回一条信息,是一个简单示例,也是一个最基本的socket编程流程. 简单步骤说明: 1.用指定的port, ip 建 ...