# springBoot-redis **Repository Path**: bruce6213/spring-boot-redis ## Basic Information - **Project Name**: springBoot-redis - **Description**: springBoot集成redis - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-11-24 - **Last Updated**: 2024-07-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 添加依赖 ``` org.springframework.boot spring-boot-starter-data-redis com.alibaba fastjson 1.2.83 org.apache.commons commons-pool2 2.9.0 ``` # 配置类 目的:自定义键值对序列化方式 ``` import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; 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; @Configuration public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // key的序列化采用StringRedisSerializer // 虽然redis的key的类型只支持字符串,但如果此处不进行字符串序列化,则会执行默认的jdk序列化:十六进制表示 StringRedisSerializer serializer = new StringRedisSerializer(); template.setKeySerializer(serializer); template.setHashKeySerializer(serializer); // value值的序列化采用fastJsonRedisSerializer (具体情况具体分析) FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); template.setValueSerializer(fastJsonRedisSerializer); template.setHashValueSerializer(fastJsonRedisSerializer); return template; } } ``` # 工具类 函数封装,方便后续调用