文章目录

  • 一、依赖
  • 二、配置文件
  • 三、RedisTemplate配置类

一、依赖

<!-- SpringBoot-redis-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis连接池 -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency>
<!-- jedis,不配置RedisTemplate、不使用jedis不需要 -->
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>

二、配置文件

spring:datasource:redis:#Redis服务器地址,默认localhost#host: 127.0.0.1#端口,默认6379#port: 6379#密码password: 123#连接几号库,默认0#database: 0#连接超时时间(毫秒)#timeout: 5000pool:#连接池最大连接数(使用负值表示没有限制)max-active: 8#连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: -1#连接池中的最大空闲连接max-idle: 8#连接池中的最小空闲连接min-idle: 0#集群cluster:#集群数量max-redirects: 3#集群ip、portnodes: 127.0.0.1:6379,127.0.0.2:6379,127.0.0.3:6379

三、RedisTemplate配置类

/*** RedisTemplate配置** @author kimi* @date 2023/1/12*/
@Configuration
public class RedisConfig {/*** 连接工厂*/@Bean//读取配置文件中redis的配置@ConfigurationProperties(prefix = "spring.datasource.redis")public JedisConnectionFactory jedisConnectionFactory() {return new JedisConnectionFactory();}/*** RedisTemplate* @param redisConnectionFactory* @return*/@Bean@ConditionalOnMissingBean(name = "redisTemplate")public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {//StringStringRedisSerializer stringRedisSerializer=new StringRedisSerializer();//JdkSerialization//JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();//Jackson2JsonJackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();//PropertyAccessor.ALL:所有;JsonAutoDetect.Visibility.ANY修饰范围:ANY-所有om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);//指定序列化输入的类型,类必须是非final修饰的om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(om);RedisTemplate<String, Object> template = new RedisTemplate<>();//连接工厂template.setConnectionFactory(redisConnectionFactory);//全局key的序列化策略template.setKeySerializer(stringRedisSerializer);//全局value的序列化策略template.setValueSerializer(jackson2JsonRedisSerializer);//全局HashKey的序列化策略template.setHashKeySerializer(stringRedisSerializer);//全局HashValue的序列化策略template.setHashValueSerializer(jackson2JsonRedisSerializer);//支持事务template.setEnableTransactionSupport(true);template.afterPropertiesSet();return template;}
}

Redis的SpringBoot配置(RedisTemplate配置)相关推荐

  1. Redis整合Springboot实现单机配置

    整体结构 配置文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  2. Spring-boot通过redisTemplate使用redis(无须手动序列化)

    转载请注明出处 : Spring-boot通过redisTemplate使用redis(无须手动序列化) redisTemplate的一些操作可以参考下面俩篇文章 http://blog.csdn.n ...

  3. SpringBoot数据访问配置

    文章目录 一.简介 二.特性 三.主要模块 四.SpringBoot整合基本JDBC与数据源 SpringBoot整合JDBC SpringBoot整合Druid数据源 一.简介 对于数据访问层而言, ...

  4. springboot的自动配置原理

    SpringBoot中的默认配置 通过刚才的学习,我们知道@EnableAutoConfiguration会开启SpringBoot的自动配置,并且根据你引入的依赖来生效对应的默认配置.那么问题来了: ...

  5. 32位数据源中没有mysql_[SpringBoot实战]快速配置多数据源(整合MyBatis)

    前言 由于业务需求,需要同时在SpringBoot中配置两套数据源(连接两个数据库),要求能做到service层在调用各数据库表的mapper时能够自动切换数据源,也就是mapper自动访问正确的数据 ...

  6. SpringBoot约定大于配置的特性解读 SpringBoot快速入门

    SpringBoot约定大于配置 Spring官方网站本身使用Spring框架开发,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置文件以及复杂的Bean依赖关系.随着Spring 3.0的 ...

  7. redis cluste(centos)配置项目可连接

    我的节点是7001-7007,三个主节点,四个备节点,用xshell操作 linux下的redis cluster每个节点配置文件redis.conf里面的"bind IP地址"是 ...

  8. SpringBoot和SpringCloud配置

    SpringBoot和SpringCloud配置 一.springBoot配置 1.1.yml 1.2.pom 1.3.RestTemplate 二.注册中心 Spring Cloud Eureka ...

  9. springboot 加载配置信息(静态)

    springboot 加载配置信息(静态) 举例:动态设置验证码过期时间 application-uat.properties配置(文件路径:/src/main/resources/applicati ...

最新文章

  1. 在Windows上使用LaTeX
  2. Odoo 菜单美化的扩展模块
  3. Content-Type简要说明
  4. mstar v56几路hdmi_Android TV : Mstar平台 GPIO 调试
  5. 【转载】C# 理解泛型
  6. 期待flash三剑客
  7. PHP Yii框架中使用smarty模板
  8. C++ _countf
  9. 系统鸿蒙生态链,任正非透露鸿蒙系统细节,比安卓速度快60%,两三年建好生态链...
  10. EMCA创建em资料库时报错
  11. linux重定向文件容加时间,[单选] linux系统,把当前日期重定向到/tmp/date文件中,并不想覆盖原来的内容,下面正确的操作是()。...
  12. Keil5(MDK与C51版本共存)下载安装
  13. linux飞行模式切换,光遇飞行的两种模式区别 飞行模式切换和区别分析
  14. Centos7操作系统搭建Snipe-IT资产管理系统
  15. selenium之鼠标操作详解
  16. web前端入门到实战:CSS角度单位:deg、grad、rad、turn
  17. 使用U盘启动安装U盘内linux6.1
  18. 这些东西不宜空腹吃[转]
  19. Programming Exercise 7: K-means Clustering and Principal Component Analysis【Maching Learning】
  20. Metasploit -- 木马生成原理和方法

热门文章

  1. 计算机服务器一般折旧年限,服务器主机折旧年限
  2. 微信支付 商品描述 body字段乱码
  3. 描述计算机网络中通信,计算机网络中,实现计算机相互通信的语言被称为(       )。...
  4. java模拟12306查询余票
  5. linux 无法识别NTFS格式的u盘
  6. 简单实用的图片边框效果
  7. PySC2 --- 星际争霸Ⅱ 学习环境搭建
  8. HTML链接打开腾讯QQ聊天窗口
  9. word转html包含公式和图片
  10. Hive教程(04)- Hive数据类型