content: package com.www.config.redis;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.www.common.exception.CustomException;
  import com.www.utils.RedisUtil;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  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.Jackson2JsonRedisSerializer;
  import org.springframework.data.redis.serializer.StringRedisSerializer;
  
  /**
   * Created with IntelliJ IDEA 2021.
   *
   * @Author: Mr Qin
   * @Date: 2024/03/24/22:14
   * @Description:        redis序列化配置
   */
  @ConfigurationProperties(prefix = "redis")
  @Configuration
  public class RedisConfig {
      //读取配置文件中的redis的ip地址
      private String host;
      private int port;
      private int database;
  
      @Bean
      RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
          RedisTemplate redisTemplate = new RedisTemplate();
          redisTemplate.setConnectionFactory(redisConnectionFactory);
  
          Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
          jackson2JsonRedisSerializer.setObjectMapper(new ObjectMapper());
  
          // 设置键和值的序列化
          redisTemplate.setKeySerializer(new StringRedisSerializer());
          redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  
          // 设置hash键和值的序列化
          redisTemplate.setHashKeySerializer(new StringRedisSerializer());
          redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  
          return redisTemplate;
      }
  
  }
  