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 ...
随机推荐
- Linux安装docker-compose
下载:curl -L https://get.daocloud.io/docker/compose/releases/download/1.16.1/docker-compose-`uname -s` ...
- BASE64Encoder cannot be resolved to a type类似问题的解决办法
我们有时候会碰见在JDK或者其他jar包中提供了某方法或者类,接口,而开发工具仍然报红,无法使用该类或者方法,那是应为我们的编译器没有识别,或者没有支持某些方法或者类 这个时候需要我们手动的开启.具体 ...
- 一种简单的REST API接口加密实现,只允许自己的产品调用后台,防止接口被刷
在项目上线后,后台接口很容易通过抓包工具看到, 难免被人为构造恶意请求攻击我们的系统,相信大家都或多或少都遇到过短信验证码被刷.疯狂留言灌水.数据被恶意爬取等问题,这种直接抓接口然后写个循环调用的行为 ...
- C/C++ 变量的本质分析
1. 程序通过变量来申请和命名内存空间. int a=0; //申请一个大小为int型的内存空间,这个内存空间取名叫a,申请的内存空间里存储0 2. 可以通过变量名访问变量的内存空间. 3. 修改变量 ...
- 页面一刷新让文本框自动获取焦点-- 和自定义v-focus指令
<body> <div id="app"> <input type="text" value="" id=&q ...
- 垃圾收集器GC
(1)DefNew(串行)收集器 Serial(串行)垃圾收集器是最基本.发展历史最悠久的收集器:JDK1.3.1前是HotSpot新生代收集的唯一选择: 特点: (1) 针对新生代采用复制算法,单线 ...
- [Linux]线程分离状态的理解
在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死:在被其他线程回收之前,它的存储器资源(如栈)是不释放的.相反, ...
- 201871010102-常龙龙《面向对象程序设计(java)》第十三周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- Cpp 二叉树
#include<vector> #include<iostream> using namespace std; //二叉树的一个节点结构 struct BinaryTreeN ...
- Vue 知识点个人总结
Vue 脚手架 脚手架 3 的版本 ---- webpack 4 cnpm install -g @vue/cli-----全局安装组件 vue create myapp-----命令行创建项目 或者 ...