整合redis
前提:
安装redis
官方进行下载
开启redis
修改redis.conf文件
整合
1、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置文件
spring:
redis:
host: 127.0.0.1
port: 6379
//这里我的密码为空,按照自己的密码来
password:
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 500
min-idle: 0
lettuce:
shutdown-timeout: 0
3、测试实体类Days
package com.sch.entity;
import java.io.Serializable;
//测试redis用
public class Days implements Serializable {
private String openId;
private String daysId;
//每天的标题
private String title;
//代办事项的数量
private int itemNumber;
//日程
private String date;
public Days() {
}
public Days(String openId, String daysId, String title, int itemNumber, String date) {
this.openId = openId;
this.daysId = daysId;
this.title = title;
this.itemNumber = itemNumber;
this.date = date;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getDaysId() {
return daysId;
}
public void setDaysId(String daysId) {
this.daysId = daysId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getItemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
4、配置类 Redis.config
package com.sch.config;
import com.sch.entity.Days;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
public RedisTemplate<String, Days> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Days> template = new RedisTemplate<>();
//配置工厂
template.setConnectionFactory(factory);
//配置序列化: 序列化是用于对象转json序列
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Days.class));
return template;
}
}
5、test文件
package com.sch;
import com.sch.entity.Days;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTest {
//此处只能用@Resource,不能用@Autowired
@Resource
private RedisTemplate<String, Days> redisTemplate;
private Days d;
@Before
public void before(){
d = new Days();
d.setDate("123");
d.setDaysId("456");
d.setItemNumber(123);
d.setOpenId("dawda");
d.setTitle("title");
}
@Test
public void testSet(){
this.redisTemplate.opsForValue().set("days",d);
System.out.println(redisTemplate.opsForValue().get("days"));
}
}
参考:https://blog.youkuaiyun.com/qq_36781505/article/details/86612988