一、添加Maven  依赖

     <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

二.application.yml 中配置 redis 相关配置

spring:
datasource:
url: jdbc:mysql://localhost:3306/order?serverTimezone=GMT%2B8&characterEncoding=UTF-8
username: root
password: xxxxxx
redis:
host: ip
database: 0
port: 6379
password:
jdbc4.0 是不用显式的去加载驱动,如果驱动包符合 SPI 模式就会自动加载
就是说程序会自动去项目中查找是否有驱动,当然没有驱动的话自然是连接不了的

三、写一个 redis 配置类

redis 默认的是使用JDK 的序列化方式。

JdkSerializationRedisSerializer

当保存实体后,Redis 中的实体信息乱码,需要修改redis 默认的序列化方式,创建RedisConfig 配置文件

@Configuration
public class RedisConfig { //设置过期时间
public static Duration liveTime = Duration.ofDays(1); @Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory){
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(liveTime)
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
.disableCachingNullValues();
RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
.cacheDefaults(cacheConfiguration).transactionAware().build();
return redisCacheManager;
} @Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory); //细化序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); //修改默认序列化器
/* Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
template.setDefaultSerializer(serializer);*/
return template;
} }

四、使用注解的方式使用Redis

@Service
public class EmpService { @Autowired
private EmpMapper empMapper; @Cacheable(cacheNames = {"emp"})
public Emp getEnp(int id){
return empMapper.getEmpById(id);
} @CachePut
public int insertEmp(Emp emp){
return empMapper.insertEmp(emp);
} @CacheEvict(beforeInvocation = false,value = "emp")
public int delEmp(int id){
empMapper.delEmp(id);
return 1;
} @CachePut(key = "#p0.id",value = "emp")
public Emp updateEmp(Emp emp){
empMapper.updateEmp(emp);
return emp;
} }

  

Spring boot 2.x 中使用redis的更多相关文章

  1. 阿里P7级教你如何在Spring Boot应用程序中使用Redis

    在Spring Boot应用程序中使用Redis缓存的步骤: 1.要获得Redis连接,我们可以使用Lettuce或Jedis客户端库,Spring Boot 2.0启动程序spring-boot-s ...

  2. Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程

    Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...

  3. spring boot 学习(六)spring boot 各版本中使用 log4j2 记录日志

    spring boot 各版本中使用 log4j2 记录日志 前言 Spring Boot中默认日志工具是 logback,只不过我不太喜欢 logback.为了更好支持 spring boot 框架 ...

  4. Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题【转】

    Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题 http://blog.didispace.com/Spring-Boot-And-Feign- ...

  5. Spring Boot源码中模块详解

    Spring Boot源码中模块详解 一.源码 spring boot2.1版本源码地址:https://github.com/spring-projects/spring-boot/tree/2.1 ...

  6. Spring Boot 计划任务中的一个“坑”

    计划任务功能在应用程序及其常见,使用Spring Boot的@Scheduled 注解可以很方便的定义一个计划任务.然而在实际开发过程当中还应该注意它的计划任务默认是放在容量为1个线程的线程池中执行, ...

  7. IDEA问题之“微服务启动项目时,不会加载Spring Boot到Services中”

    1.启动项目时,不会加载Spring Boot到Services中 现象解析: 启动项目时 会在debug的位置加载项目 注:这里没有配图,因为问题已解决,未记录图,需往后遇到记录 解决方案: 需要在 ...

  8. Spring Boot 1.5.4集成Redis

    本文示例源码,请看这里: 如何安装与配置Redis,请看这里 首先添加起步依赖: <dependency> <groupId>org.springframework.boot& ...

  9. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

随机推荐

  1. Go递归

    1. 递归介绍 package main import ( "fmt" ) func test(n int) { if n > 2 { n-- test(n) } fmt.P ...

  2. springboot的安装与初步使用

    1.引用springboot框架 1.在maven项目底下的pom.xml的中,引用springboot,如下 <?xml version="1.0" encoding=&q ...

  3. 安装docker并使用docker安装mysql

    安装Docker 1. Docker 教程地址:https://www.runoob.com/docker/centos-docker.install.html 2.安装docker 命令:yum i ...

  4. Redis安装及局域网访问配置CentOS

    1.安装gcc,用来编译reids 通过命令 sudo yum install gcc 2.安装redis $ wget http://download.redis.io/releases/redis ...

  5. flex一行显示两列(CSS)

    外层display: flex;flex-wrap: wrap; 里层 width:49%

  6. vue基础总结

    Vue语法: new Vue({ //挂载: el: '#app', //初始化数据: data: {}, //监听 data 的数据变化: watch: { todos: { //深度监视 hand ...

  7. PTA点赞狂魔

     点赞狂魔 (25 分) 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每篇博文都有一些刻画其特性的标签,而你点赞的博文的类型,也间接刻画了你的特性.然而有这么一种人,他们会通过给自己看 ...

  8. vs2019 opencv4的相关配置

    opencv4.11存在改动,导致许多demo没有办法正常运行,但是配置方法却是相同的. 主要是连接器输入,头文件包含路径,库路.如果想要调试,还需要设置调试文件符号表. [未完待续]

  9. LA 3708 墓地雕塑(模拟)

    题目链接:https://vjudge.net/problem/UVALive-3708 这道题的思路也是比较难想. 首先根据上一题(Uva 11300)可知,要想让移动距离最短,那么至少要使一个雕塑 ...

  10. 解决1130-host'192.168.2.137'is not allowed to connect to this mysql server报错问题

    连接数据库服务器出现1130-host'192.168.2.137'is not allowed to connect to this mysql server错误, 这个问题是因为在数据库服务器中的 ...