所有文章只是记录工作中用到的一些东西,方便日后自己在别的项目中快速上手,下面开始springboot和redis的整合。
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置aplication.yml
spring:
redis:
host: 192.168.1.8
password: xSjXY^0,GQeJ4*35
3.编写RedisConfig类
@Configuration
@EnableCaching//开启注解
public class RedisConfig {
/**
* 配置redisTemplate,主要指定key和value的序列化方式
* @param connectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<Object, Object> template=new RedisTemplate<Object, Object>();
template.setConnectionFactory(connectionFactory);
//实现序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
//实现序列化和反序列化redis的value值,默认使用JdkSerializationRedisSerializer
//template.setValueSerializer(new RedisObjectSerializer());
/