Springboot与Redis的整合
构建springboot的maven项目并添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
创建实体类,并实现Serializable接口序列化
例如,创建Student实体类:
public class Student implements Serializable {
private static final long serialVersionUID = 7314816257503984761L;
private long id;
private String name;
private Date joinTime;
//省略getter和setter
}
注意:注意添加serialVersionUID
,,否则实体类序列化到redis中,再取出来时,不能正确反序列化解析数据
配置redis
在springboot的配置文件中添加 :
spring:
redis:
host: localhost
port: 6379
database: 0
使用RedisTemplate
对redis进行操作
//...
import org.springframework.data.redis.core.RedisTemplate;
//...
@RestController
public class RouterController {
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("/set")
public void set(@RequestBody Student student) {
/**
* POST http://localhost:8080/set
* content-type:application/json
*
* {
* "id":10014,
* "name":"zhangsan",
* "joinTime":"2020-04-15"
* }
*/
//将一个student对象序列化,存储到redis, 对应的key为student
redisTemplate.opsForValue().set("student", student);
}
@GetMapping("/get/{key}")
public Student get(@PathVariable("key") String key) {
/**
* GET http://localhost:8080/get/student
*/
//从redis中获取key为"student"的值
return (Student) redisTemplate.opsForValue().get(key);
}
@DeleteMapping("/del/{key}")
public boolean del(@PathVariable("key") String key) {
/**
* DELETE http://localhost:8080/del/student
*/
//从Redis中删除key为student的键值,返回一个boolean,成功即true,否则为false
return redisTemplate.delete(key);
}
}
Redis的5种基本数据类型的操作
字符串
@GetMapping("/string")
public String string() {
redisTemplate.opsForValue().set("string", "hello,redis!");
String str = (String) redisTemplate.opsForValue().get("string");
return str;
}
列表
@GetMapping("/list")
public List<String> list(){
ListOperations listOperations=redisTemplate.opsForList();
//letfPush Or rightPush
listOperations.leftPush("list", "redis");
listOperations.leftPush("list", "java");
listOperations.leftPush("list", "spring");
listOperations.leftPush("list", "java");
listOperations.leftPush("list", "mysql");
/**
* listOperations.range(key, start, end)
* key:键
* start:截取范围的起始下标
* end:截取范围结束的下标
*/
List<String> list=listOperations.range("list",0,3);
return list;
}
集合
@GetMapping("/set")
public Set<String> set() {
SetOperations setOperations = redisTemplate.opsForSet();
// 第一个参数为key,后面多个参数为value,
setOperations.add("set", "java", "redis", "spring", "java", "springboot");
// 或者
setOperations.add("set", "mysql");
setOperations.add("set", "mybatis");
// 获取set
Set<String> set = setOperations.members("set");
return set;
}
有序集合
@GetMapping("/zset")
public Set<String> zset() {
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset", "java", 2);
zSetOperations.add("zset", "spring", 3);
zSetOperations.add("zset", "springboot", 1);
zSetOperations.add("zset", "redis", 0);
// 获取zset
Set<String> zset = zSetOperations.range("zset", 0, 3);
return zset;
}
哈希
@GetMapping("/hash")
public void hash() {
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("hash1", "username", "张三");
hashOperations.put("hash1", "id", 10001);
// 或者
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", "李四");
map.put("id", 10002);
hashOperations.putAll("hash2", map);
// 获取key为hash1中hashkey的值
String hash1_username = (String) hashOperations.get("hash1", "username");
Integer hash1_id = (Integer) hashOperations.get("hash1", "id");
// 获取key为hash2中hashkey的值
String hash2_username = (String) hashOperations.get("hash2", "username");
Integer hash2_id = (Integer) hashOperations.get("hash2", "id");
// 打印
System.out.println("hash1_username" + hash1_username + ",hash1_id:" + hash1_id);
System.out.println("hash2_username" + hash2_username + ",hash2_id:" + hash2_id);
}