依赖
<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报错: