spring boot使用总结(四)在spring boot中使用redis

 

   1) 想要在spring boot中使用redis,只要在pom文件中增加如下的配置就可以了。

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 

    2) 引入spring-boot-redis jar包之后,选择相应的客户端配置连接就可以使用redis了,推荐使用Jedis。

@Bean
JedisConnectionFactory connectionFactory() {
    return new JedisConnectionFactory();
}

 

    3)配置完连接后,就可以使用redis进行简单的存取了。如果存取的key,value都是String的,可以使用

     RedisTemplate<String, String>对象,该对象由spring boot自动扫描生成,在需要的地方可以直接注入。

     如果只是进行简单的存储,还可以生成ValueOperations对象来方便操作。

 

@Bean
ValueOperations<String, String> strOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForValue();
}

 

    4)如果想要存取其他类型的对象,例如存取key为string,value为Integer类型的对象,可以new这个对象,       然后配置连接即可使用。

 

@Bean
RedisTemplate<String, Integer> intRedisTemplate(JedisConnectionFactory connectionFactory) {
    RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>();
    redisTemplate.setConnectionFactory(connectionFactory);
    return redisTemplate;
}

 

    5)RedisTemplate对象默认使用jdkSerializer实现序列化,如果想要更换序列化的实现方式,例如使用json实现     value的序列化,可以进行如下配置

 

@Bean
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
            Object.class);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    return jackson2JsonRedisSerializer;
}

@Bean
RedisTemplate<String, Object> objRedisTemplate(JedisConnectionFactory connectionFactory,
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(connectionFactory);
    redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringRedisSerializer);
    redisTemplate.setHashKeySerializer(stringRedisSerializer);
    return redisTemplate;
}

 

    最后附上上述操作的完整代码

 

@Configuration
public class RedisConfig {

    @Bean
    JedisConnectionFactory connectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    ValueOperations<String, String> strOperations(RedisTemplate<String, String> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    RedisTemplate<String, Integer> intRedisTemplate(JedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>();
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }

    @Bean
    ValueOperations<String, Integer> intOperations(RedisTemplate<String, Integer> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
                Object.class);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }

    @Bean
    RedisTemplate<String, Object> objRedisTemplate(JedisConnectionFactory connectionFactory,
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        return redisTemplate;
    }

    @Bean
    ValueOperations<String, Object> objOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

}

 

展开阅读全文

没有更多推荐了,返回首页