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. bzoj2688 Green Hackenbush

    (没有嘟嘟嘟) 权限题,请各位自己想办法交.不过代码正确性是可以保证的,至于为啥那不能说. 刚学完卡特兰数,就给我这种神题,我除了知道\(n\)个点的不同形态二叉树的数目是卡特兰数外,别的就不会了. ...

  2. .netcore signalR 实时消息推送

    服务器端引入包 Install-Package Microsoft.AspNetCore.SignalR客户端引入包  npm install @aspnet/signalr <template ...

  3. 第二次博客作业: 函数+进制转换器v1.0beta

    一:运行截图  二:介绍函数 1, int panduan1(int n,char a[],int count,int sign)//判断用户是否输入了除数字和a-f范围外的字符 { int i; ; ...

  4. Spring基础环境搭建所需要的jar包

    红色标明的jar包.是spring框架开发的基础jar包. 必要jar包. spring-core-4.1.6.RELEASE.jar 框架核心jar包. spring-beans-4.1.6.REL ...

  5. ReactiveCocoa实践

    1.按钮addTarget [[self.aDepositBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNe ...

  6. mongodb 数据更新命令、操作符

    一.Mongodb数据更新命令 Mongodb更新有两个命令:update.save. 1.1update命令 update命令格式: db.collection.update(criteria,ob ...

  7. js图片转base64编码

    let reader = new FileReader(); reader.readAsDataURL(file.raw); reader.onload = () => { let imageU ...

  8. multiprocessing.Pool报pickling error

    multiprocessing.Pool报pickling error 现象 multiprocessing.Pool传递一个普通方法(不在class中定义的)时, 能正常工作. from multi ...

  9. PHP判断访问者是PC端还是移动端

    function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) { ...

  10. VMware安装Centos7超详细过程

    本篇文章主要介绍了VMware安装Centos7超详细过程(图文),具有一定的参考价值,感兴趣的小伙伴们可以参考一下 一.软硬件准备 软件:推荐使用VMwear,我用的是VMwear 12 镜像:Ce ...