Redis
非关系型数据库,灵活、存储速度快。
基于内存进行存储,支持key-value的存储形式,底层使用c语言编写的。
基于key-value形式的数据字典,结构非常简单,没有数据表的概念,直接用键值对的形式完成数据的管理,Redis支持5中数据类型:
-
字符串
-
列表
-
集合
-
有序集合
-
哈希
安装Redis
启动Redis服务
sudo ./bin/redis-server ./etc/redis.conf
set name "Redis"
get name
shutdown
- 退出客户端:control+c
Spring Boot整合Redis(CRUD)
-
创建Maven工程
-
在pom.xml文件中引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mvc依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring Data Redis 启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--引入客户端,通过客户端操作redis-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- 引入lombok简化实体类开发-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
@Data
//实现序列化接口,才能够存入到Redis中,基于内存存储
public class Student implements Serializable{
private Integer id;
private String name;
private Double score;
private Date birthday;
}
@RestController
public class StudentHandleer{
//import org.springframework.data.redis.core.Redistemplate
@Autowrited
pirvate Redistemplate redistemplate
/**
*添加学生(修改和添加类似,只需传入相同的key即可)
*/
@PostMapper("/set")
//@RequestBody把客户端传来的json转为java对象
public void set(@RequestBody Student student){
redistemplate.opsForValue().set("student",student);
}
/**
*查询学生
*/
@GetMapping("/get/{key}")
public Student get(@PathVariable("key") String key){
return (Student)redistemplate.opsForValue().get(key);
}
/**
*删除学生
*/
@DeleteMapping("/delete/{key}")
public boolean delete(@PathVariable("key")String key){
redistemplate.delete(key);
return redistemplate.hasKey(key);
}
}
spring:
redis:
database:0
host: localhost
prot: 6379
- 创建启动类(Application.java)
@SpringBootApplication
public class Application{
public static void main(String args[]){
SpringApplication.run(Application.class,args)
}
}
- postMan测试接口,必须json格式
Redis存储时名字前面追加序列化字符,取出反序列化。(存序列化,取反序列化)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。