一、添加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. 【Python】第一个程序---Helloworld!

    对于大多数程序语言,第一个入门编程代码便是"Hello World!",以下代码为使用Python输出"Hello World!": #!/usr/bin/py ...

  2. 【Python】使用socketserver建立一个异步TCP服务器

    概述 这篇文章是讲解如何使用socketserver建立一个异步TCP服务器,其中Python版本为3.5.1. socketserver主要的类 socketserver模块中的类主要有以下几个:1 ...

  3. python代码在linux终端中执行报错:Unable to init server: Could not connect: Connection refused

    python代码在linux终端中执行时报错: Unable to init server: Could not connect: Connection refused Unable to init ...

  4. git密码相关问题

    一.解决:每次都需要输入账号密码 git config --global credential.helper store 二.后期git密码更改后,重置密码操作 git config --system ...

  5. 关于XMlHttpRequest对象

    //创建XMLHttpRequest对象的三种方法 1 var xhr = createXMLHttpRequest(); function createXMLHttpRequest(){ try{ ...

  6. Linux下查看当前文件大小的命令

    1.ls -lht 列出每个文件的大小和当前目录所有文件大小总和 2.du -sh * 列出当前文件夹下的所有子文件的大小 看你需要啥样的,自己来吧

  7. Laravel Vuejs 实战:开发知乎 (4)实现找回密码

    资料 : Resetting Passwords   以及 Episode 35 - The Password Reset Flow 由于之前的实现里默认自带重置找回密码功能,不再复述. 默认的重置页 ...

  8. MySQL导出数据到文件报错

    执行如下语句: mysql> select * from users into outfile "F:\Develop\MySQL57\Uploads\users.txt" ...

  9. JQuery选择器&过滤器

    JQuery对象: JQuery对象的本质上是DOM数组,它对DOM元素进行了封装 JQuery对象和JavaScript对象可以互转(\$()/$obj()[i]),但是JQuery对象和Javas ...

  10. ubuntu16.04安装node.js、npm

    ubuntu16.04安装node.js.npm1.请尽量避免在 Ubuntu 上使用 apt-get 来安装 node.js, 如果你已经这么做了,请手动移除: sudo apt-get purge ...