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 ...
随机推荐
- jQuery 源码分析(二十) DOM操作模块 插入元素 详解
jQuery的DOM操作模块封装了DOM模型的insertBefore().appendChild().removeChild().cloneNode().replaceChild()等原生方法.分为 ...
- 分布式的cap原理
由来 1998年的加州大学的计算机科学家 Eric Brewer 提出,分布式有三个指标. Consistency,Availability,Partition tolerance. 简称即为CAP. ...
- Python内部参数中对外部实参的影响
无论函数传递的参数的可变还是不可变,只要针对参数使用赋值语句,会在函数内部修改局部变量的引用,不会影响到外部变量的引用,而如果传递的参数是可变类型,在函数内部使用方法修改了数据的内容,同样会影响到外部 ...
- llinux/重启/用户切换/注销
一.指令 shutdown命令 shutdown -h now //立即关机 shutdown -h 2 //分钟后关机 shutdown -r now //立即重启 shutdown -r 1 // ...
- NIO基础方法一
1.remaining();返回当前位置与limit之间得元素数. int[] intArray={1,2,3,4}; IntBuffer intBuffer=IntBuffer.wrap(intAr ...
- Android Gradle 学习笔记(二):Gradle Wrapper
Wrapper,就是对Gradle的一层包装,便于在团队开发过程中统一Gradle构建的版本.这样大家就可以使用统一的Gradle版本进行构建,避免因为Gradle的版本不统一带来的不必要的问题. 这 ...
- 从0系统学Android--2.6 Activity 的最佳实践
从0系统学Android--2.6 Activity 的最佳实践 本系列文章目录:更多精品文章分类 本系列持续更新中.... 实践中的技巧 2.6.1 知晓当前是在哪个 Activity 这个其实很简 ...
- vscode中js文件失去高亮/没有智能提示
vscode中js文件失去高亮/没有智能提示 两步: 第一步:基本的语法高亮提示,需要将vetur删掉,然后把vscode的历史记录缓存删掉,重启vscode. 第二步:js的智能提示,使用插件typ ...
- KumuluzEE - Java EE的微服务框架
KumuluzEE - Java EE的微服务架构 https://www.jdon.com/soa/kumuluzEE.html
- SQL Server通过函数把逗号分隔的字符串拆分成数据列表的脚本-干货
CREATE FUNCTION [dbo].[Split](@separator VARCHAR(64)=',',@string NVARCHAR(MAX)) RETURNS @ResultTab ...