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的更多相关文章

  1. IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统

    先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...

  2. SpringBoot简单整合redis

    Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...

  3. springboot+jpa+mysql+redis+swagger整合步骤

    springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...

  4. springBoot(8)---整合redis

    Springboot整合redis 步骤讲解 1.第一步jar导入: <dependency> <groupId>org.springframework.boot</gr ...

  5. SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本

    背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...

  6. 【SpringBoot】整合Redis实战

    ========================9.SpringBoot2.x整合Redis实战 ================================ 1.分布式缓存Redis介绍 简介: ...

  7. SpringBoot学习(七)—— springboot快速整合Redis

    目录 Redis缓存 简介 引入redis缓存 代码实战 Redis缓存 @ 简介 redis是一个高性能的key-value数据库 优势 性能强,适合高度的读写操作(读的速度是110000次/s,写 ...

  8. 完整SpringBoot Cache整合redis缓存(二)

    缓存注解概念 名称 解释 Cache 缓存接口,定义缓存操作.实现有:RedisCache.EhCacheCache.ConcurrentMapCache等 CacheManager 缓存管理器,管理 ...

  9. SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

随机推荐

  1. 2019 Multi-University Training Contest 10

    目录 Contest Info Solutions C - Valentine's Day D - Play Games with Rounddog E - Welcome Party G - Clo ...

  2. codeforces722E

    CF722E Research Rover Unfortunately, the formal description of the task turned out to be too long, s ...

  3. 二十、网络ifconfig 、ip 、netstat、ss之二

    ip 网络层协议 ip地址 点分十进制分为4段,范围 0-255 ip分类 A 占据1段,最左侧一段第一位固定为0 0 000 0000 - 0 111 1111  0 - 127:其中0为网络,12 ...

  4. vue draggable 火狐拖拽搜索问题

    最近在使用vuedraggable做导航时候,谷歌拖拽是没问题的,但是在火狐测试时候,拖拽时候是可以成功,但是火狐还是打开了一个新的tab,并且搜索了,一开始想着是阻止默认行为,但是在@end时间中阻 ...

  5. redis数据结构有哪些

    1.String 可以是字符串,整数或者浮点数,对整个字符串或者字符串中的一部分执行操作,对整个整数或者浮点执行自增(increment)或者自减(decrement)操作. 2.list 一个链表, ...

  6. python3 django连接mysql数据库

    在django中将模型类中的数据迁移到mysql数据库中,首先使用pip install pymysql安装pymysql库, 然后在项目中的__init__.py中添加 import pymysql ...

  7. 冲刺阶段——Day2

    [今日进展] 完成黄金点游戏的算法与代码架构. 使用文字界面完成任务 码云链接:https://gitee.com/jxxydwt1999/20175215-java/blob/master/Gold ...

  8. IDEA 好用的插件

    IDEA 好用的插件 非自带的一些自用插件. Alibaba java Coding Guidelines 阿里出的java规范,支持eclipse和Idea,支持实时扫描,规范代码,养成良好习惯.推 ...

  9. ExtractIcon function Retrieves a handle to an icon from the specified executable file, DLL, or icon file.

    https://msdn.microsoft.com/zh-cn/vstudio/ms648068(v=vs.90)

  10. Servlet的三种实现方式

    A: 实现Servlet接口 B: 继承GenericServlet C: 继承HttpServlet,它是对Http协议进行了封装