当前位置: 首页 > article >正文

【Spring Boot】如何集成Redis

  1. 在pom.xml文件中导入spring data redis的maven坐标。
    1.         <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-redis</artifactId>
              </dependency>
  2. 在application.yml文件中加入redis相关配置。
    1. spring:
        redis:
          host: 192.168.xxx.xxx
          port: 6379
          password: xxxx
          database: 0
  3. 在Bean管理中加入配置类RedisConfig。
    1. package com.app.studypro.config;
      
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.cache.annotation.CachingConfigurerSupport;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.redis.connection.RedisConnectionFactory;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.data.redis.serializer.StringRedisSerializer;
      
      /**
       * Redis的配置信息
       *
       * @author Administrator
       */
      @Configuration
      @Slf4j
      public class RedisConfig extends CachingConfigurerSupport {
      
          @Bean
          public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
              RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
              log.info("Redis的KeySerializer设置为:{}", StringRedisSerializer.class);
              // 默认的Key序列化器为:JdkSerializationRedisSerializer
              // 将key的序列化器改为StringRedisSerializer,以便可以在Redis的key设置什么就显示什么,不进行转化
              redisTemplate.setKeySerializer(new StringRedisSerializer());
              redisTemplate.setConnectionFactory(connectionFactory);
              return redisTemplate;
          }
      
      }
      

http://www.kler.cn/news/136269.html

相关文章:

  • SwiftUI:单个App支持设置多语言
  • 【云原生网关】Higress 从部署到使用详解
  • Linux-Centos操作系统备份及还原(整机镜像制作与还原)--再生龙
  • kubeadm快速自动化部署k8s集群
  • 人工智能的未来:重塑生活与工作的变革者
  • h5页面与小程序页面互相跳转
  • 【心得】基于flask的SSTI个人笔记
  • Oracle 数据库中 查询时如何使用日期(时间)作为查询条件
  • 使用Python的turtle模块绘制玫瑰花图案(含详细Python代码与注释)
  • C++二分查找算法:132模式枚举3简洁版
  • IDEA版SSM入门到实战(Maven+MyBatis+Spring+SpringMVC) -Maven依赖管理,版本号管理,继承和聚合
  • 软件测试/测试开发/人工智能丨基于Spark的分布式造数工具:加速大规模测试数据构建
  • Wireshark抓包:理解TCP三次握手和四次挥手过程
  • Redis-高性能原理剖析
  • IDEA自动注解设置(中文版)
  • 密码加密解密之路
  • openssl C++研发之pem格式处理详解
  • springboot使用MongoTemplate根据正则表达式查询日期数据
  • 计算机算法分析与设计(23)---二分搜索算法(C++)
  • 设计模式 -- 建造者模式(Builder Pattern)
  • 广州华锐互动VRAR | VR课件内容编辑器解决院校实践教学难题
  • 中贝通信-603220 三季报分析(20231120)
  • 【python】直方图正则化详解和示例
  • Fourier分析导论——第6章——R^d 上的Fourier变换(E.M. Stein R. Shakarchi)
  • 【C++】使用std::vector()函数实现矩阵的加、减、点乘、点除等运算
  • 【腾讯云 HAI域探秘】高性能服务器引领AI革新浪潮:从AI绘画、知识问答到PyTorch图像分类、视频检测的全方位探索