Redis配置类

天行健,君子以自强不息;地势坤,君子以厚德载物。


每个人都有惰性,但不断学习是好好生活的根本,共勉!


文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。

Redis配置类

  • Redis配置类1
  • Redis配置类2


在使用redis时,会用到RedisTemplate工具,需要配置配置类,以此来连接工厂、序列化存入的键值。
这里配置了两个不同的配置类,内容大致形同,细节略有不同,可挑选使用。

Redis配置类1

具备:序列化、工厂连接、键值字符串序列化、hash键值字符串序列化

package com.data.config;

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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassDescription:
 * @Author:李白
 * @Date:2023/7/25 14:44
 */
@Configuration
public class RedisTemplateConfig {

    @Bean(name = "redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        System.out.println("redis序列化-->");
        RedisTemplate redisTemplate = new RedisTemplate();
        RedisSerializer redisSerializer = new StringRedisSerializer();
        //连接工厂
        redisTemplate.setConnectionFactory(factory);
        //键序列化
        redisTemplate.setKeySerializer(redisSerializer);
        //值序列化
        redisTemplate.setValueSerializer(redisSerializer);
        //key hashMap序列化
        redisTemplate.setHashKeySerializer(redisSerializer);
        //value hashMap序列化
        redisTemplate.setHashValueSerializer(redisSerializer);
        return redisTemplate;
    }

}

Redis配置类2

具备:序列化、工厂连接、键值序字符串列化、hash键值Json序列化

package com.data.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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;

/**
 * @ClassDescription:
 * @Author:李白
 * @Date:2023/7/25 14:44
 */
@Configuration
public class RedisTemplateConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}


你可能感兴趣的:(#,Redis,redis,数据库)