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 ...
随机推荐
- JVM备忘点(1.8以前)
1.内存结构 左边两个线程共享,右边三个线程私有. 方法区:.class文件的类信息.常量.static变量.即时编译器编译后的代码(动态代理).HotSpot将方法区称为永久代 堆:分为新生代和老年 ...
- Selenium处理授权框,登录框,握手框
在Web自动化中,常常遇到三种弹框: JavaScript警告框,如alert,confirm,prompt等,如下图: 处理方法为driver.switch_to.alert().dismiss() ...
- Django基础之ModelForm
1. form与model的终极结合 class BookForm(forms.ModelForm): class Meta: model = models.Book fields = "_ ...
- 解决ScrollView中Recyclerview显示不全,滑动不流畅的问题
这个问题经常会碰到,看了下网上关于这个问题的解决方案,有很多都是复制粘贴的,并不能根本解决问题 比较有效的一种方案是在Recyclerview的外层套一个RelativeLayout 然后设置recy ...
- 2019balsn两道web和2019巅峰极客一道web记录
遇到3道有点意思的web,记录一下~ web1 题目地址:http://warmup.balsnctf.com/ 源码如下所示: <?php if (($secret = base64_deco ...
- sql注入笔记-sqlite
1. SQLite 1. 常用语句及基本结构 (1)sqlite因为其比较简易每个db文件就是一个数据库,所以不存在information_schema数据库,但存在类似作用的表sqlite_mast ...
- dd 命令
dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: if=文件名:输入文件名 ...
- IDEA中log4j.properties配置文件详解
配置实例 ### 配置根 ### log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,D ...
- kafka和rabbitmq对比
1.吞吐量kafka吞吐量更高:1)Zero Copy机制,内核copy数据直接copy到网络设备,不必经过内核到用户再到内核的copy,减小了copy次数和上下文切换次数,大大提高了效率.2)磁盘顺 ...
- SQL-W3School-高级:SQL UNION 和 UNION ALL 操作符
ylbtech-SQL-W3School-高级:SQL UNION 和 UNION ALL 操作符 1.返回顶部 1. SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT ...