首页>代码>SpringBoot+Vue前后端分离学校教学管理系统>/yxt/后端代码/yxt/src/main/java/com/zl/yxt/config/RedisConfig.java
package com.zl.yxt.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;

@Configuration
public class RedisConfig {

    @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;
    }


}
最近下载更多
顾北城  LV12 3月28日
西瓜哥哥  LV4 2023年8月8日
ericxu1116  LV24 2023年5月28日
1806795436  LV4 2023年1月10日
331376387  LV5 2022年11月16日
qlpqlp  LV7 2022年11月14日
郎建伟  LV1 2022年11月11日
wensente  LV2 2022年11月2日
nyfcalf  LV12 2022年10月14日
最近浏览更多
mortal的i人 4月11日
暂无贡献等级
wmxhahah  LV7 4月8日
denliv_hui  LV13 4月1日
顾北城  LV12 3月28日
jc121140  LV3 3月22日
玖零定制问题修复  LV34 3月3日
胡晓阳  LV2 1月17日
Anzhui  LV2 1月12日
13870342 1月11日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友