springBoot 自定义redisTemplate

package com.atirm.mybatismutiplesource.config.RedisConfig;

import com.atirm.mybatismutiplesource.entity.User;
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 java.net.UnknownHostException;

/**
* 配置自定义的redisTemplate,并设置自动的序列化
*/
@Configuration
public class RedisConfig {

@Bean
public RedisTemplate<Object, User> userRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, User> template = new RedisTemplate<Object, User>();
template.setConnectionFactory(redisConnectionFactory);
// 默认是的用jdk序列化的,需要改成json
Jackson2JsonRedisSerializer<User> ser = new Jackson2JsonRedisSerializer<User>(User.class);
template.setDefaultSerializer(ser);
return template;
}
}


posted @ 2019-03-06 10:28  雷神约  阅读(689)  评论(0编辑  收藏  举报