添加依赖:
<!--添加redis依赖,该依赖里默认包含了spring-data-redis和Jedis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
实体类:
package com.example.demo.part3.chapter8.redis;
import java.io.Serializable;
public class Girl implements Serializable{
//必须使用时间序列化接口,因为使用jackson做序列化需要一个空构造
private static final long serialVersionUID=1l;
private int id;
private String name;
private String addr;
private int age;
public Girl(int id, String name, String addr, int age) {
this.id = id;
this.name = name;
this.addr = addr;
this.age = age;
}
public Girl() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Girl{" +
"id=" + id +
", name='" + name + '\'' +
", addr='" + addr + '\'' +
", age=" + age +
'}';
}
}
package com.example.demo.part3.chapter8.redis;
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 javax.annotation.Resource;
@Repository
public class GirlDao {
@Autowired
//Springboot已经为我们配置好了StringRedisTemplate,在此处可以直接注入。
StringRedisTemplate stringRedisTemplate;
@Resource(name = "stringRedisTemplate")
//使用resource注解指定stringRedisTemplate,可注入基于字符串的简单属性操作方法
ValueOperations<String,String> valueOpsStr;
@Autowired
//Springboot已经为我们配置好了redisTemplate,在此处可以直接注入。
RedisTemplate<Object,Object> redisTemplate;
@Resource(name = "redisTemplate")
//使用resource注解指定redisTemplate,可注入基于对象的简单属性操作方法
ValueOperations<Object,Object> valOps;
//通过set方法,存储字符串类型
public void stringRedisTemplateDemo(){
valueOpsStr.set("xx","yy");
}
//通过set方法,存储对象类型
public void save(Girl girl){
valOps.set(girl.getName(),girl);
}
//通过get方法,获取字符串
public String getString(){
return valueOpsStr.get("xx");
}
//通过get方法,获取对象
public Girl getGirl(){
return (Girl)valOps.get("lisi");
}
}
入口类:
package com.example.demo;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cache.annotation.EnableCaching;
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 java.net.UnknownHostException;
@SpringBootApplication//核心注解,主要是开启自动配置
//@EntityScan(basePackages = "com.example.**")
//开启缓存支持
//@EnableCaching
public class DemoApplication {
//项目启动的入口
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
//配置redistemplate并定义serializer
@Bean
@SuppressWarnings({"rawtypes","unchecked"})
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory
redisConnectionFactory)throws UnknownHostException{
RedisTemplate<Object,Object> template=new RedisTemplate<Object,Object>();
template.setConnectionFactory(redisConnectionFactory);
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);
//设置值的序列化采用jackson2JsonRedisSerializer
template.setValueSerializer(jackson2JsonRedisSerializer);
//设置键的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
控制层:
package com.example.demo.part3.chapter8.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/redisCon")
public class RedisController {
@Autowired
GirlDao girlDao;
@RequestMapping("/set")
//演示设置字符及对象
public void set(){
Girl girl=new Girl(1,"lisi","shibei",18);
girlDao.save(girl);
girlDao.stringRedisTemplateDemo();
}
@RequestMapping("/getStr")
//演示获得字符
public String getStr(){
return girlDao.getString();
}
@RequestMapping("/getGirl")
//演示获得对象
public Girl getGirl(){
return girlDao.getGirl();
}
}
测试:
1.向redis写入数据:
2.查看redis数据库:
3.getStr:
4.gerObj: