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 ...
随机推荐
- I Hate It (HDU 1754)
Problem 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师 ...
- MySQL备忘点(下)
联结表 创建联结 FROM 表1,表2 与内连接作用相同类似:如果失去WHERE子句,会出现笛卡尔积现象 内联结 INNER JOIN 高级联结 自联结 例子:SELECT 字段b FROM 表 WH ...
- 7月清北学堂培训 Day 3
今天是丁明朔老师的讲授~ 数据结构 绪论 下面是天天见的: 栈,队列: 堆: 并查集: 树状数组: 线段树: 平衡树: 下面是不常见的: 主席树: 树链剖分: 树套树: 下面是清北学堂课程表里的: S ...
- [python]函数默认参数顺序问题
python 函数参数定义有四类: 1.必选参数:调用函数时候必须赋值的参数. a,须以正确的顺序传入函数b,调用时的数量必须和声明时的一样 def exa(x): return x #b作为参数进入 ...
- vue的一些随笔
一.点击路由后的样式,可以在路由文件index.js中设置 再在样式里面设置active的类名对应的样式. ———————————————————————————————————————————— 二 ...
- vue 中引入第三方js库
以 jQuery 为例 一.绝对路径直接引入,全局可用 主入口页面 index.html 中用 script 标签引入: <script src="./static/jquery-1. ...
- 在testrpc以太坊测试环境部署智能合约
2018年03月13日 09:20:54 思无邪-machengyu 阅读数 2683 版权声明:本文为博主原创文章,转载请务必注明出处,否则追究法律责任 https://blog.csdn.ne ...
- Handler常见两种用法
1.Handler在Android的两个功能 1.1表示未来某时做某事 1.2线程间通信 2.演示源码如下: package com.example.datastrorage; import andr ...
- Dart 语法中文在线学习网址收藏
为了学习flutter UI框架,必须先学好dart语言,故收藏了有关 Dart 语法中文在线学习网址 http://dart.goodev.org/guides/language/language- ...
- kotlin数据解构
fun main(arg: Array<String>) { val person = person("tom") var (name) = person//解构 pr ...