springboot集成redis
1.配置文件如下
spring: 
# 热部署
 devtools:
  restart:
    enabled: true  #设置开启热部署
    additional-paths: src/main/java #重启目录
    exclude: WEB-INF/**
  freemarker:
    cache: false    #页面不加载缓存,修改即时生效
     # redis
 redis:
   host: 192.168.0.6
   port: 6479
   timeout: 5000  # 连接超时时间(毫秒)  
   # password:
   pool:
     minIdle: 0   # 连接池中的最小空闲连接 
     maxIdle: 8   # 连接池中的最大空闲连接  
     maxWait: -1  # 连接池最大阻塞等待时间(使用负值表示没有限制) 
     maxActive: 8  # 连接池最大连接数(使用负值表示没有限制) 
2.引入data-redisjar包
 <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
3.写redis配置类
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //使用fastjson序列化
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
 @GetMapping("/getdata")
    public void getdata(){
        //测试redis
        redisUtil.set("name","小猫");
        redisUtil.set("age", "11", 15);
        System.out.println(redisUtil.getExpire("name"));
        System.out.println( redisUtil.get("name"));
        System.out.println(redisUtil.getExpire("age"));
        System.out.println( redisUtil.get("age"));
}
4.配置好了写简单测试类

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号