Spring-boot通过redisTemplate使用redis(无须手动序列化)

redisTemplate的一些操作可以参考下面俩篇文章
http://blog.csdn.net/whatlookingfor/article/details/51863286
http://www.jianshu.com/p/7bf5dc61ca06


导入redis依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

Jackson依赖

<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>2.3.2version>
dependency>

配置application.yml配置文件

# REDIS (RedisProperties)
spring:
    redis:
        database: 0      # Redis数据库索引(默认为0)
        host: localhost  # Redis服务器地址
        port: 6379       # Redis服务器连接端口
        password:        # Redis服务器连接密码(默认为空)
        timeout: 0       # 连接超时时间(毫秒)
        pool:
          max-active: 8  # 连接池最大连接数(使用负值表示没有限制)
          max-idle: 8    # 连接池中的最大空闲连接
          max-wait: -1   # 连接池最大阻塞等待时间(使用负值表示没有限制)
          min-idle: 0    # 连接池中的最小空闲连接

使redisTemplate无须手动序列化的类,并用jackson替换默认的序列化工具

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;

/**
 * Created by zhengwei on 2017/10/17.
 */
@Configuration
public class RedisConfig {

    /**
     * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 使用Jackson2JsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // 设置value的序列化规则和 key的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

开始使用

import com.example.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {

    @Autowired
    private RedisTemplate template;

    @Test
    public void contextLoads() {
        User user = new User(1,"象拔蚌");
        template.opsForValue().set(user.getId()+"",user);
        //原本opsForValue()是只能操作字符串的.现在就可以操作对象了
        User result = (User) template.opsForValue().get(user.getId()+"");
        System.out.println(result.toString());
    }
}

输出结果:

User{id=1, name='象拔蚌'}

剩下的很多操作就自己去百度探索吧

你可能感兴趣的:(spring-boot)