ch07-SpringBoot 集成 Redis
Redis 是一个 NoSQL 数据库, 常作用缓存 Cache 使用。 通过 Redis 客户端可以使用多种语言在程序中,访问 Redis 数据。java 语言中使用的客户端库有 Jedis,lettuce, Redisson等。
Spring Boot 中使用 RedisTemplate 模版类操作 Redis 数据。
1.1 SpringBoot 集成 Redis 步骤
1.1.1 启动 redis-server.exe
1.1.2 加入 Maven 依赖
<dependencies>
<!--redis 起步依赖-->
<!--spring boot 会在容器中创建两个对象 RedisTemplate ,StringRedisTemplate-->
<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>
</dependencies>
1.1.3 修改 application.properties 文件
#指定 redis
spring.redis.host=localhost
spring.redis.port=6379
server.port=9001
server.servlet.context-path=/myboot
1.1.4 创建 RedisController
package com.suyv.controller;
import com.suyv.vo.Student;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.security.PublicKey;
@RestController
public class RedisController {
// 注入 RedisTemplate
// 泛型 key,value 都是 String ,或者 Object, 不写
// 注意:RedisTemplate对象的名称redisTemplate
@Resource
private RedisTemplate redisTemplate;
// 添加数据到redis
@PostMapping("/redis/add")
public String addToRedis(String name,String value){
// 操作Redis中的String类型的数据,先获取ValueOperations对象
ValueOperations valueOperations = redisTemplate.opsForValue();
// 添加数据到Redis
valueOperations.set("myname","lisi");
return "向Redis添加String类型数据";
}
// 获取redis数据
@GetMapping("/redis/get")
public String selectRedis(String key){
ValueOperations valueOperations = redisTemplate.opsForValue();
Object obj = valueOperations.get(key);
return "查询Redis中的数据:key="+ key + ",value=" + obj;
}
}
1.1.5 添加测试
1.1.6 查询测试
1.1.7 Redis Desktop Manager 客户端查看
1.2 StringRedisTemplate 和 RedisTemplate
StringRedisTemplate:把k,v 都是作为String处理,使用的是String的序列化,可读性好。
RedisTemplate:把k,v 经过了序列化存到redis。 k,v 是序列化的内容, 不能直接识别。默认使用的jdk序列化,可以修改为前提的序列化。
1.2.1 修改 Controller 方法
package com.suyv.controller;
import com.suyv.vo.Student;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.security.PublicKey;
/**
* @author suyv
* @date 2022年01月23日 19:48
*/
@RestController
public class RedisController {
@Resource
private StringRedisTemplate stringRedisTemplate;
@PostMapping("/redis/{k}/{v}")
public String addStringKV(@PathVariable String k,
@PathVariable String v){
// 使用StringRedisTemplte对象
stringRedisTemplate.opsForValue().set(k,v);
return "使用StringRedisTemplte对象";
}
@GetMapping ("/redis/{k}")
public String getStringValue(@PathVariable String k){
// 使用StringRedisTemplte对象
String v = stringRedisTemplate.opsForValue().get(k);
return "使用StringRedisTemplte对象:key=" + k + ",value=" + v;
}
}
1.2.2 添加测试
1.2.3 查询测试
1.2.4 Redis Desktop Manager 客户端查看
1.3 序列化与反序列化
序列化:把对象转化为可传输的字节序列过程称为序列化。
反序列化:把字节序列还原为对象的过程称为反序列化。
1.为什么需要序列化
2.什么情况下需要序列化
3.序列化的方式
1.3.1 创建 Student 实体类
package com.suyv.vo;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 3780814096368075484L;
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
1.3.2 修改 Controller 文件
package com.suyv.controller;
import com.suyv.vo.Student;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.security.PublicKey;
@RestController
public class RedisController {
@Resource
private RedisTemplate redisTemplate;
@Resource
private StringRedisTemplate stringRedisTemplate;
// 设置RedisTemplate的序列化
// 可以设置key的序列化,也可以设置value的序列化,也可以两者都设置
@RequestMapping("/redis/addStr")
public String addString(String k,String v){
// 使用 RedisTemplate
// 设置 key 使用 String 序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 设置 value 使用 String 序列化
redisTemplate.setValueSerializer(new StringRedisSerializer());
return "定义RedisTemplate对象的key,value的序列化";
}
//使用json序列化,把java对象转为json对象
@PostMapping("/redis/addJson")
public String addJson(){
Student student = new Student();
student.setId(1001);
student.setName("zhangsan");
student.setAge(20);
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 把值作为json序列化
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));
redisTemplate.opsForValue().set("mystudent",student);
return "json序列化";
}
@PostMapping("/redis/getJson")
public String getJson(){
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 把值作为json序列化
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));
Object obj = redisTemplate.opsForValue().get("mystudent");
return "json反序列化" + obj;
}
}
1.3.3 RedisTemplate 序列化测试
1.3.4 Java对象 转 Json 对象测试