SpringBoot(十一):SpringBoot整合Redis
详解springboot整合redis:https://blog.csdn.net/qq_36781505/article/details/86612988

一、环境准备
- Redis-x64-3.2.100.zip
- SpringBoot 1.5.10.RELEASE
Redis-x64-3.2.100.zip 下载地址:https://github.com/MicrosoftArchive/redis/releases
pom依赖:
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二:application.yml:
spring:
redis:
host: 120.79.82.191
port: 6379
password: Lysb_TestRedis!2019
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 500
min-idle: 0
lettuce:
shutdown-timeout: 0 datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.20:3306/test?useSSL=false
username: root
password: root123 logging.level.com.demo.mapper: debug
bean:
package cn.demo.bean;
import java.io.Serializable;
public class Days implements Serializable {
private String openId;
private String daysId;
//每天的标题
private String title;
//代办事项的数量
private int itemNumber;
//日程
private String date;
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getDaysId() {
return daysId;
}
public void setDaysId(String daysId) {
this.daysId = daysId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getItemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "Days [openId=" + openId + ", daysId=" + daysId + ", title=" + title + ", itemNumber=" + itemNumber
+ ", date=" + date + "]";
}
}
redisconfig:
package cn.demo.redis.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import cn.demo.bean.Days; @Configuration
public class RedisConfig{
@Bean
public RedisTemplate<String, Days>redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,Days>template=new RedisTemplate<>();
//关联
template.setConnectionFactory(factory);
//设置key的序列化器
template.setKeySerializer(new StringRedisSerializer());
//设置value的序列化器
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Days.class));
return template;
}
}
redisTest:
package cn.demo.test; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner; import cn.demo.bean.Days; @RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest { @Autowired
private RedisTemplate<String,Days> redisTemplate; //注入一个对象缓存在redis
@Test
public void testSet(){
Days d=new Days();
d.setDate("123");
d.setDaysId("456");
d.setItemNumber(123);
d.setOpenId("dawda");
d.setTitle("title");
this.redisTemplate.opsForValue().set("days",d);
System.out.println("redisTemplate=="+(redisTemplate.opsForValue().get("days")));
}
}
SpringBoot(十一):SpringBoot整合Redis的更多相关文章
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制
一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...
- redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)
平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...
- 25、springboot与缓存整合Redis
默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中 docker: 安装Redis: 查看官方文档: 添加约束 <dependency> <gro ...
- SpringBoot学习:整合Redis
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 pom.xml添加对redis的依赖: <!-- https://mvnrepos ...
- springboot笔记10——整合Redis
依赖 <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boo ...
- springboot 2.x整合redis,spring aop实现接口缓存
pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- SpringBoot + MySQL + MyBatis 整合 Redis 实现缓存操作
本地安装 Redis Redis 安装:https://www.cnblogs.com/oukele/p/11373052.html 项目结构: SpringBootRedis 工程项目结构如下: ...
- SpringBoot:Shiro 整合 Redis
前言 前段时间做了一个图床的小项目,安全框架使用的是Shiro.为了使用户7x24小时访问,决定把项目由单机升级为集群部署架构.但是安全框架shiro只有单机存储的SessionDao,尽管Shrio ...
- springboot(十一)SpringBoot任务
github地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 1 ...
随机推荐
- 【数据结构】什么是二叉查找树(BST)
什么是二叉查找树(BST) 1. 什么是BST 对于二叉树中的每个节点X,它的左子树中所有项的值都小于X中的项,它的右子树中所有项的值大于X中的项.这样的二叉树是二叉查找树. 以上是一颗二叉查找树,其 ...
- [错误]Caused by: org.apache.spark.memory.SparkOutOfMemoryError: Unable to acquire 65536 bytes of memory, got 0
今天,在运行Spark SQL代码的时候,遇到了以下错误: Caused by: org.apache.spark.SparkException: Job aborted due to stage f ...
- C++ `endl` 与 `\n` 的区别
std::cout << std::endl : 插入换行并刷新缓存区 (flush the buffer) std::cout << "\n" : 插入换 ...
- 第一篇随笔:用VB.NET搞点简单事情(1)
网络上能搜索到的爬虫文章大多是用python做的,也有少部分是C#做的(小声:所以用VB.NET也可以做爬虫.本文写的是第一步:获取网页) 使用代码前先imports以下内容 Imports Syst ...
- 【React Native】react-native之集成支付宝支付、微信支付
一.在使用支付宝支付.微信支付之前导入桥接好的头文件 github地址:https://github.com/xujianfu/react-native-pay 二.集成支付宝支付流程 RN支付宝需要 ...
- SQL 高效运行注意事项(三)
合理配置tempdb 1.tempdb在SQL Server停掉,重启时会自动的drop,re-create. 根据model数据库会默认建立一个新的 2.tempdb对IO的要求比较高,最好分配到高 ...
- 读书笔记_python网络编程3_(3)
3.TCP:传输控制协议 第一个版本在1974年定义,建立在网际层协议(IP)提供的数据包传输技术之上.TCP使程序可以使用连续的数据流进行相互通信. 除非网络原因导致连接中断/冻结,TCP都能保证将 ...
- Git操作删除 untracked files
最近使用git 管理项目的时候,编译过程中出现了很多中间文件,今天发现使用 git clean 命令可以很方便进行清除: # 删除 untracked files git clean -f # 连 u ...
- Python语法速查: 1. 数据类型与内置函数
返回目录 (1)常用内置数据类型 分类 类型名称 描述 数字 int 整数 float 浮点数 complex 复数 bool 布尔值 序列 str 字符串(不可变序列) list 列表 tuple ...
- s3c2440裸机-时钟编程(二、配置时钟寄存器)
s3c2440裸机编程-时钟编程(二.配置时钟寄存器) 1.2440时钟时序 下图是2440时钟配置时序: 1.上电后,nRESET复位信号拉低,此时cpu还无法取指令工作. 2.nRESET复位信号 ...