
即Redis Desktop Manager 进行连接,连接步骤如下

验证处填写你的密码即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Book
package com.huang.demo.demo.model;
import java.util.Date;
public class Book {
private Integer id;
private String name;
private String author;
private Date publishDate;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", publishDate=" + publishDate +
'}';
}
public Book(Integer id, String name, String author, Date publishDate) {
this.id = id;
this.name = name;
this.author = author;
this.publishDate = publishDate;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public Book() {
}
public Book(Integer id, String name, String author) {
this.id = id;
this.name = name;
this.author = author;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
@Test
void test02() {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set("k3", "v3");
String k3 = ops.get("k3");
System.out.println("k3 = " + k3);
}

@Test
void test03() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("b1", new Book(1, "三国演义3", "罗贯中"));
Book b1 = (Book) ops.get("b1");
System.out.println("b1 = " + b1);
}
linux上

@Test
void test01() {
//序列化
redisTemplate.setKeySerializer(RedisSerializer.string());
ValueOperations op = redisTemplate.opsForValue();
op.set("k2", "v2");
Object k2 = op.get("k2");
System.out.println("k2 = " + k2);
}

@Test
void contextLoad2() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println("k1 = " + k1);
}

@Test
void test03() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("b1", new Book(1, "三国演义3", "罗贯中"));
Book b1 = (Book) ops.get("b1");
System.out.println("b1 = " + b1);
}

@Test
void test05() {
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
ValueOperations ops = redisTemplate.opsForValue();
//存的时候,系统会自动将 book 对象转为 JSON 字符串存到 Redis 中
ops.set("b2", new Book(99, "红楼梦5", "曹雪芹"));
//从 Redis 中读取的时候,读到的也是 JSON 字符串,会自动将 JSON 转为一个 Book 对象
Book b2 = (Book) ops.get("b2");
System.out.println("b2 = " + b2);
}

@Test
void contextLoads() {
redisTemplate.setKeySerializer(RedisSerializer.string());
ObjectMapper om = new ObjectMapper();
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(om));
ValueOperations ops = redisTemplate.opsForValue();
ops.set("b2",new Book(9,"红楼梦","曹雪芹",new Date()));
Book b2 = (Book) ops.get("b2");
System.out.println("b2 = " + b2);
}

封装原因:由于要多次大量的使用到json对象,(带不带时间类型多加几行代码就行),但是由上面的几个案例可以得出结论是,前面的几行都是重复的,故此我们把多余重复的代码抽出来封装起来
按两下shift 输入搜索即可

代码主题,把中间这一段抽取处来

即
这一段抽取出来,放入我们的工具类RedisConfig中

执行封装的步骤:

package com.huang.demo.demo.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.text.SimpleDateFormat;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate();
//为时间类型加的代码段,转化为SimpleDateFormat格式
ObjectMapper om = new ObjectMapper();
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(RedisSerializer.string());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
Test
// 封装之后
@Test
void test1(){
ValueOperations ops = redisTemplate.opsForValue();
//存的时候,系统会自动将 book 对象转为 JSON 字符串存到 Redis 中
ops.set("b12", new Book(99, "红楼梦", "曹雪芹",new Date()));
//从 Redis 中读取的时候,读到的也是 JSON 字符串,会自动将 JSON 转为一个 Book 对象
Book b2 = (Book) ops.get("b12");
System.out.println("b12 = " + b2);
}
