一个Spring Boot集成Redis的小案例
- 新建Spring Boot项目
搭建Spring Boot项目,依赖为Redis和Web。
- 领域模型类
package com.jxufe.redisProj1.domain;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private Integer age;
public Person() {
super();
// TODO 自动生成的构造函数存根
}
public Person(String id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
注意:此类必须用时间序列化接口,因为使用Jackson做序列化需要一个空构造。
- 数据访问层
package com.jxufe.redisProj1.dao;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import com.jxufe.redisProj1.domain.Person;
@Repository
public class PersonDao {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Resource(name = "stringRedisTemplate")
private ValueOperations<String, String> valOpsStr;
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
@Resource(name = "redisTemplate")
private ValueOperations<Object, Object> valOps;
public void stringRedisTemplateDemo() {
valOpsStr.set("xx", "yy");
}
public void save(Person person) {
valOps.set(person.getId(), person);
}
public String getString() {
return valOpsStr.get("xx");
}
public Person getPerson() {
return (Person) valOps.get("1");
}
}
- 配置
Spring Boot为我们自动配置了RedisTemplate,而RedisTemplate使用的是JdkSerializationRedisSerializer,这个对我们演示Redis Client很不直观,因为其使用二进制形式存储数据,在此我们将自己配置RedisTemplate,并定义Serializer。
package com.jxufe.redisProj1;
import java.net.UnknownHostException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@SpringBootApplication
public class RedisProj1Application {
public static void main(String[] args) {
SpringApplication.run(RedisProj1Application.class, args);
}
@Bean
@SuppressWarnings({ "rawtypes", "unchecked" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException{
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
- 控制器
package com.jxufe.redisProj1.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jxufe.redisProj1.dao.PersonDao;
import com.jxufe.redisProj1.domain.Person;
@RestController
public class DataController {
@Autowired
private PersonDao personDao;
@RequestMapping("/set")
public void set() {
Person person = new Person("1","wzy",21);
personDao.save(person);
personDao.stringRedisTemplateDemo();
}
@RequestMapping("/getStr")
public String getStr() {
return personDao.getString();
}
@RequestMapping("/getPerson")
public Person getPerson() {
return personDao.getPerson();
}
}
- 运行
访问http://localhost:8080/set,此时查看Redis Client。
访问http://localhost:8080/getStr,页面显示情况如下:
访问http://localhost:8080/getPerson,页面情况如下所示: