springboot+JPA 整合redis
1.导入redis依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
2.配置redis参数:
#redis配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=1
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait= -1ms
spring.redis.jedis.pool.max-idle=500
3.编写redis工具类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import springbootd.demo.entity.User; import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit; @Repository
public class RedisUtil { @Autowired
private StringRedisTemplate template; @Autowired
private RedisTemplate<String,Object> redisTemplate; public void testSetKey(String key,Object value){
ValueOperations<String,Object> ops =redisTemplate.opsForValue();
ops.multiSet(value);
} public User testGetValue(String key){
ValueOperations<String,Object> ops =redisTemplate.opsForValue();
return (User) ops.get(key);
} public void setKey(String key ,String value){
ValueOperations<String,String> ops = template.opsForValue();
ops.set(key,value,1, TimeUnit.MINUTES);
} public String getValue(String key){
ValueOperations<String,String> ops = this.template.opsForValue();
return ops.get(key);
}
}
5.测试:
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.test.context.junit4.SpringRunner;
import springbootd.demo.entity.User;
import springbootd.demo.util.RedisUtil; @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests { @Test
public void contextLoads() {
} @Autowired
RedisUtil redisUtil; @Test
public void testRedis(){
redisUtil.setKey("userName","chen");
redisUtil.setKey("age","18"); User user = new User();
user.setId((long) 2);
user.setUserName("xiaomi");
user.setPassWord("123");
redisUtil.testSetKey("user_2",user); System.out.println(redisUtil.testGetValue("user_2").toString()); System.out.println("用户名"+redisUtil.getValue("userName"));
System.out.println("年龄"+redisUtil.getValue("age")); }
}
redis如果要缓存实体类的话,此实体类必须序列化:
public class User implements Serializable{
}
或者使用gson工具进行实体类和json字符串之间转换:
1.gson依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.4</version>
</dependency>
转换方法:
@Test
public void testRedis(){ Gson gson =new Gson(); User user = new User();
user.setId((long) 3);
user.setUserName("xiaomi");
user.setPassWord("123");
redisUtil.testSetKey("user_3",gson.toJson(user)); String userStr =redisUtil.testGetValue("user_3"); System.out.println(gson.fromJson(userStr,User.class).toString()); }
Redis官方没有提供Window版本,不过微软维护了一个版本,下载地址为:https://github.com/MSOpenTech/redis/releases,下载.msi版本进行安装。
springboot+JPA 整合redis的更多相关文章
- IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统
先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...
- SpringBoot简单整合redis
Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...
- springboot+jpa+mysql+redis+swagger整合步骤
springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...
- springBoot(8)---整合redis
Springboot整合redis 步骤讲解 1.第一步jar导入: <dependency> <groupId>org.springframework.boot</gr ...
- SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本
背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...
- 【SpringBoot】整合Redis实战
========================9.SpringBoot2.x整合Redis实战 ================================ 1.分布式缓存Redis介绍 简介: ...
- SpringBoot学习(七)—— springboot快速整合Redis
目录 Redis缓存 简介 引入redis缓存 代码实战 Redis缓存 @ 简介 redis是一个高性能的key-value数据库 优势 性能强,适合高度的读写操作(读的速度是110000次/s,写 ...
- 完整SpringBoot Cache整合redis缓存(二)
缓存注解概念 名称 解释 Cache 缓存接口,定义缓存操作.实现有:RedisCache.EhCacheCache.ConcurrentMapCache等 CacheManager 缓存管理器,管理 ...
- SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中
在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...
随机推荐
- Noip2011 提高组 选择客栈
P1311 选择客栈 直通 思路: ①看题,我们可以发现一个显然的性质,即当最左边的客栈向右移动时,最右边的客栈时单调向右的,并且右端点往右的客栈也符合要求.(因为只要左侧有一个满足的,右边的自然可以 ...
- windows下注册表脚本编写
Reg文件就是我今天所说的注册表脚本文件,双击可将其中的数据写入注册表.利用注册表脚本文件可以对注册表进行关于键值的任何操作,而且还不受注册表被禁用的限制. 我们平常对注册表的修改大体上可以分 ...
- 【CTS2019】珍珠【生成函数,二项式反演】
题目链接:洛谷 pb大佬说这是sb题感觉好像有点过fan...(我还是太弱了) 首先,设$i$这个数在序列中出现$a_i$次,要求$\sum_{i=1}^D[a_i \ mod \ 2]\leq n- ...
- vue中使用echarts画饼状图
echarts的中文文档地址:https://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20EC ...
- java.util.Calendar获取时间区间问题
虽然java8的LocalDate已经出来,但是很多项目以及自己习惯上还是使用Date,这里还是简单介绍一下如何通过java.util.Calendar获取时间区间. 1 通过calendar.get ...
- Yarn 安装 node-sass 依赖导致 Build Fresh Packages 太慢的问题
解决办法: 1. 在 项目目录下新建 .yarnrc 文件 添加以下代码 registry "https://registry.npm.taobao.org" sass_binar ...
- pod package 生成 Framework
pod package 生成 Framework pod package 是 cocoapods 的一个插件,如果没有的话使用以下命令安装: sudo gem install cocoapods-pa ...
- MYSQL数据库基础概念
数据库的发展史 1.萌芽阶段:文件系统 使用磁盘文件来存储数据2.初级阶段:第一代数据库 出现了网状模型.层次模型的数据库3.中级阶段:第二代数据库 关系型数据库和结构化查询语言4.高级阶段:新一代数 ...
- APP界面架构设计
作为PM,信息架构和页面流的设计想必烂熟于心,当确定好产品战略层和范围层即为何种目标用户提供何种服务后,就要着手搭建功能架构,将目标功能通过良好的用户体验传递给用户,目的是高效解决用户痛点,从而实现价 ...
- PostgreSQL SELECT INTO和INSERT INTO SELECT 两种表复制语句
SELECT INTO和INSERT INTO SELECT两种表复制语句都可以用来复制表与表之间的数据,但是它们之间也有区别. 建表语句: bas_custom_rel表 CREATE TABLE ...