spring boot 集成redis

依赖

<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.7.1.RELEASE</version>
    </dependency>

<dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.7.3</version>
</dependency>

package com.jf.redis;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonAnyFormatVisitor;
import com.jf.entity.Student;

/**
 * @Description
 * @Author zengzhiqiang
 * @Date 2019年1月30日
 */
@Repository
public class StudentRedis {

    //@Autowired
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    
    public void add(String key,Long time ,Student student){
       redisTemplate.opsForValue().set(key,JSON.toJSONString(student), time, TimeUnit.MINUTES);
    }
    
    public void add(String key, Long time, List<Student> students) {
        redisTemplate.opsForValue().set(key,JSON.toJSONString(students), time, TimeUnit.MINUTES);
    }

    public Student get(String key) {
        Student user = null;
        String userJson = redisTemplate.opsForValue().get(key);
        if(!StringUtils.isEmpty(userJson))
            user = JSON.parseObject(userJson, Student.class);
        return user;
    }

    public List<Student> getList(String key) {
        List<Student> ts = null;
        String listJson = redisTemplate.opsForValue().get(key);
        if (!StringUtils.isEmpty(listJson))
            ts = JSON.parseArray(listJson, Student.class);
        return ts;
    }

    public void delete(String key){
        redisTemplate.opsForValue().getOperations().delete(key);
    }
}

 

package com.jf.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, String> redisTemplate(
            RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

 

application.yml

spring:
  redis:
  # database: 1
    host: localhost
    port: 6379
    pool:
      max-idle: 8
      min-idle: 0
      max-active: 8
      max-wait: -1

 这里采用的是localhost ,如果采用ip地址,确保此ip拥有读写权限

 

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

此引入有版本限制

 ==============

测试

    @RequestMapping("/hello2")
    public Student  hello2(){
        
        Student s = new Student();
        s.setId("999999");
        s.setTimed(new Date());    
        s.setDes("22555");
        s.setName("zzq");
        
        studentRedis.add("yy_"+s.getName(), 10L, s);
        System.out.println("add a student ok ");
        Student sget = studentRedis.get("yy_zzq");
        System.out.println("get a student ok name "+sget.getName());
        return s;
    }

当然你也可以写junit

=================

<dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.7.3</version>
</dependency>

没有引入项目不会报错,但是运行失败,如下

 

SpringBoot和redis集成的时候,CachingConfigurerSupport 的子类RedisConfig中,RedisConnectionFactory factory报错:

https://blog.csdn.net/qq_25188255/article/details/78550217

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值