springboot项目集成redis

c1own      2022-05-17     353

关键词:

一、在pom文件中添加依赖

1 <!-- 集成redis -->
2 <dependency>
3      <groupId>org.springframework.boot</groupId>
4      <artifactId>spring-boot-starter-data-redis</artifactId>
5 </dependency>

二、在application.yml文件中配置redis

 1 spring:
 2   redis:
 3     host: 127.0.0.1 #redis服务器IP地址
 4     port: 6379 #端口号
 5     jedis:
 6       pool:
 7         max-active: 8 #最大连接数
 8         max-idle: 8 #最大空闲数
 9         min-idle: 0 #最小空闲数
10         max-wait: 30000 #最大等待时间 ms(毫秒)
11     timeout: 50000 #连接超时时间 ms(毫秒)
12     database: 0 #连接的数据库

三、创建RedisConfig文件

 1 package com.ruoyi.web.core.config;
 3 import org.springframework.cache.annotation.CachingConfigurerSupport;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.data.redis.connection.RedisConnectionFactory;
 7 import org.springframework.data.redis.core.RedisTemplate;
 8 import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
 9 import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
10 import org.springframework.data.redis.serializer.StringRedisSerializer;
11 
12 /**
13  * @ClassName RedisConfig
14  * @Descrption redis序列配置
15  * @Author wm
16  * @Date 2020/1/11
17  * @Version 1.0
18  */
19 @Configuration
20 public class RedisConfig extends CachingConfigurerSupport {
21 
22     @Bean
23     public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
24         //创建RedisTemplate对象
25         RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
26         //设置连接工厂
27         redisTemplate.setConnectionFactory(redisConnectionFactory);
28         //序列化对象
29         //简单的字符串序列化
30         StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
31         //jdk序列化
32         JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
33         //Json序列化
34         GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer  = new GenericJackson2JsonRedisSerializer ();
35         //设置String键的序列化方式
36         redisTemplate.setKeySerializer(stringRedisSerializer);
37         //设置String值的序列化方式
38         redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
39         return redisTemplate;
40     }
41 }

四、例子

 1 package com.ruoyi.web.controller.test;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 import java.io.Serializable;
 6 
 7 @Component
 8 public class Test01 implements Serializable {
 9 
10     private static final long serialVersionUID = 4332651162911740406L;
11     private String userName;
12     private String password;
13     private String phoneNum;
14 
15     public String getUserName() {
16         return userName;
17     }
18 
19     public void setUserName(String userName) {
20         this.userName = userName;
21     }
22 
23     public String getPassword() {
24         return password;
25     }
26 
27     public void setPassword(String password) {
28         this.password = password;
29     }
30 
31     public String getPhoneNum() {
32         return phoneNum;
33     }
34 
35     public void setPhoneNum(String phoneNum) {
36         this.phoneNum = phoneNum;
37     }
38 }
TestController
 1 import com.ruoyi.zhibowei.service.attendance.IAttendRecordEmployeeStatisService;
 2 import org.springframework.beans.factory.annotation.Autowired;
 3 import org.springframework.data.redis.core.RedisTemplate;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 
 9 import javax.servlet.http.HttpServletRequest;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.TimeUnit;
13 
14 /**
15  * @author 
16  * @description TODO
17  * @create 
18  */
19 @Controller
20 @RequestMapping("/test")
21 public class TestTestController {
22 
23     @Autowired
24     private IAttendRecordEmployeeStatisService attendRecordEmployeeStatisService;
25     @Autowired
26     private RedisTemplate<String,Object> redisTemplate;
27     @RequestMapping("/testqj")
28     public void testQJAttend(){
29 
30     }
31 
32 
33     @RequestMapping("/demo")
34     @ResponseBody
35     public String[] demo(HttpServletRequest request){
36         List<Test01> userList = new ArrayList<Test01>();
37 
38         Test01 user1 = new Test01();
39         user1.setUserName("风雅颂");
40         user1.setPassword("aw916132444");
41         user1.setPhoneNum("13874877512");
42 
43         Test01 user2 = new Test01();
44         user2.setUserName("水雷屯");
45         user2.setPassword("xf82652215");
46         user2.setPhoneNum("15576758485");
47 
48         Test01 user3 = new Test01();
49         user3.setUserName("天山遁");
50         user3.setPassword("sd555292222");
51         user3.setPhoneNum("13515114556");
52 
53         userList.add(user1);
54         userList.add(user2);
55         userList.add(user3);
56         redisTemplate.opsForValue().set("userList",userList,60, TimeUnit.SECONDS);
57         return null;
58     }
59 
60     @RequestMapping(value="getValue",method={RequestMethod.GET,RequestMethod.POST})
61     @ResponseBody
62     public Object getValue(){
63         Object object = redisTemplate.opsForValue().get("userList");
64         return object;
65     }
66 
67 }

输出结果

技术图片

springboot2.x集成redis缓存(代码片段)

SpringBoot集成Redis缓存在此章,我们将SpringBoot集成Redis缓存,Redis是一个开源的,基于内存的数据结构存储,可以用作数据库、缓存和消息代理,在本章仅讲解缓存集成。准备工作当前项目工具及环境开发工具IDEA2020.3依赖管理MavenS... 查看详情

springboot项目+shiro(权限框架)+redis(缓存)集成

...户管理的服务,既是服务端也是客户端);SpringCloud框架的SpringBoot的项目搭建就不再赘述,这里重点介绍如何引入集成Shiro框架:ApacheShiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码学和会话管理。使用Shiro的易于... 查看详情

7springboot与redis集成

1.7SpringBoot与Redis集成简介继续上篇的MyBatis操作,详细介绍在SpringBoot中使用RedisCacheManager作为缓存管理器,集成业务于一体。完整源码:Spring-Boot-Demos1.7.1创建spring-boot-redis项目pom.xml文件如下:<?xmlversion="1.0"encoding="UTF-8"?><pro... 查看详情

springboot集成redis客户端傻瓜式流程

...,学习完redis的基本命令和特性后。我们要集成进我们的springboot项目中不废话上代码 在application.yml中加入spring:redis:database:0#使用的第几个数据库host:localhost#连接hostport:6379#端口password:#输入你的设置密码  pom.xml引入客户端... 查看详情

springboot集成redis配置mybatis二级缓存(代码片段)

目录写在前面源码获取一、MyBatis缓存机制1.1、一级缓存1.2、二级缓存二、集成Redis2.1、安装Redis2.2、项目引入Redis2.2.1、Maven依赖2.2.2、配置application.yml2.2.3、配置序列化规则三、配置二级缓存2.1、开启二级缓存2.2、自定义缓存类2.3... 查看详情

springboot集成redis(代码片段)

springboot整合Redisredis学习笔记狂神视频SpringBoot操作数据:spring-datajpajdbcmongodbredis!SpringData也是和SpringBoot齐名的项目!说明:在SpringBoot2.x之后,原来使用的jedis被替换为了lettucejedis:采用的直连 查看详情

springboot集成redis实现缓存(代码片段)

前言在此章,我们将SpringBoot集成Redis缓存,Redis是一个开源的,基于内存的数据结构存储,可以用作数据库、缓存和消息代理,在本章仅讲解缓存集成。一键获取源码地址准备工作当前项目工具及环境开发工具... 查看详情

springboot集成redis实现缓存(代码片段)

前言在此章,我们将SpringBoot集成Redis缓存,Redis是一个开源的,基于内存的数据结构存储,可以用作数据库、缓存和消息代理,在本章仅讲解缓存集成。一键获取源码地址准备工作当前项目工具及环境开发工具... 查看详情

springboot集成redis基础入门(代码片段)

...式的数据备份更多redis相关文档请查看redis官方文档redis和springbootredis在springboot项目开发中是常用的缓存套件,常见使用的是spring-boot-starter-data-redisspringboot 查看详情

springboot整合shiro集成redis缓存

简介:由于考虑到项目后期分布式部署,所以缓存由ehcache改为redis,而redis既有单机版部署,也有分布式部署,所以二者需要兼容。1.maven依赖<dependency><groupId>org.crazycake</groupId><artifactId>shiro-redis</artifactId><... 查看详情

springboot集成redis—进行增加,更新,查询,批量删除等操作

...,(2)更新操作与添加操作一样,接下来上代码:1.建立Springboot项目,引入Redis依赖(pom.xml如下):<?xmlversion=" 查看详情

springboot怎么快速集成redis?

前言其实在Springboot中集成redis是一个非常简单的事情,但是为什么要单独输出一篇文章来记录这个过程呢?第一个原因是,我记性不是太好,这次把这个过程记录下,在新的项目搭建的时候或者需要在本地集... 查看详情

springboot集成redis来实现缓存技术方案

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

springboot集成redis启动失败causedby:java.lang.classnotfoundexception:org.apache.commons.pool2.impl.g(代码片段)

项目场景今天在使用SpringBoot集成Redis搭建项目学习分布式锁的时候,在pom.xml文件引入Redis的相关依赖后,在配置文件application.properties进行了基础的redis配置,启动项目后,发现项目启动失败。问题描述项目的基本... 查看详情

springboot教程(十四)|springboot集成redis(全网最全)(代码片段)

...候,我们可以选择直接集成这些原生客户端。但是在springBoot中更常见的方式是集成spring-data-redis,这是spring提供的一个专门用来操作redis的项目,封装了对redis的常用操作,里边主要封装了jedis和lettuce两个客户端。... 查看详情

springboot之集成redis:springcache+redis

前言来啦老铁!笔者学习SpringBoot有一段时间了,附上SpringBoot系列学习文章,欢迎取阅、赐教:5分钟入手SpringBoot;SpringBoot数据库交互之SpringDataJPA;SpringBoot数据库交互之Mybatis;SpringBoot视图技术;SpringBoot之整合Swagger;SpringBoot之junit单... 查看详情

springboot集成redis

SpringBoot集成Redis引入依赖pom文件引入Redis依赖spring-boot-starter-data-redis<!--redis依赖配置--><dependency>  <groupId>org.springframework.boot</groupId>  <artifactId&g 查看详情

springboot集成redis(代码片段)

SpringBoot集成Redis文章目录SpringBoot集成Redis1、概述2、测试Redis3、自定义redisTemplate1、概述Redis是什么?Redis(RemoteDictionaryServer),即远程字典服务。是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日... 查看详情