1 Redis 介绍
Redis 是一个开源(BSD许可)的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件,并提供多种语言的API。
1.1 Redis 优点
- 存取速度快:Redis速度非常快,每秒可执行大约110000次的设值操作,或者执行81000次的读取操作。
- 支持丰富的数据类型:Redis支持开发人员常用的大多数数据类型,例如列表、集合、排序集和散列等。
- 操作具有原子性:所有Redis操作都是原子操作,这确保如果两个客户端并发访问,Redis服务器能接收更新后的值。
- 提供多种功能:Redis提供了多种功能特性,可用作非关系型数据库、缓存中间件、消息中间件等。
1.2 Redis 的下载安装及连接
【基础篇】一文带你掌握 Redis - 潘志的研发笔记的文章 - 知乎
链接地址:
https://zhuanlan.zhihu.com/p/593697447
这篇文章对 Redis 进行了全面的介绍,涵盖了其基本概念、安装方法、命令行客户端操作、事务与 Lua 脚本操作以及可视化客户端等方面。
2 流程
- 创建项目,引入相应的依赖启动器。
使用Spring Initializr的方式构建项目,选择依赖SQL->MyBatis Framework \ MySQL Driver 。
- 在pom文件中添加 Spring Data Redis 依赖启动器。
org.springframework.boot
spring-boot-starter-data-redis
- 编写实体类。
(1)编写实体类Person。
package com.itheima.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
import java.util.List;
@RedisHash("persons")
// 加上这个注解就会在redis中创建一个名为persons的hash数据结构(存储空间)
public class Person {
@Id // 用于表示实体类主键
private String id;
// 由于我们有可能要根据姓单独查询或者根据名单独插叙,也可能进行组合查询。
// 针对这种查询情况,要加上@Indexed
@Indexed // 用于标识该属性会在redis数据库中生成二级索引
private String firstname;
@Indexed
private String lastname;
private Address address;
private List familyList;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List getFamilyList() {
return familyList;
}
public void setFamilyList(List familyList) {
this.familyList = familyList;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
", address=" + address +
", familyList=" + familyList +
'}';
}
}
(2)编写实体类Address。
package com.itheima.domain;
import org.springframework.data.redis.core.index.Indexed;
public class Address {
@Indexed
private String city;
@Indexed
private String country;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", country='" + country + '\'' +
'}';
}
}
(3)编写实体类Family。
package com.itheima.domain;
import org.springframework.data.redis.core.index.Indexed;
public class Family {
@Indexed
private String type;
@Indexed
private String username;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "Family{" +
"type='" + type + '\'' +
", username='" + username + '\'' +
'}';
}
}
- 编写Repository接口PersonRepository。
package com.itheima.repository;
import com.itheima.domain.Person;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.domain.Page;
import java.util.List;
// <当前操作实体类,主键数据类型>
public interface PersonRepository extends CrudRepository{
// 1.根据姓氏查询某人
List findByLastname(String lastname);
// 2.根据姓氏查询某人,采用分页的格式。
Page findPersonByLastname(String lastname, Pageable page);
// 3.根据姓氏和名字查询某些人
List findByFirstnameAndLastname(String firstname, String lastname);
// 4.查询某座城市居住的所有人
List findByAddress_City(String city);
// 5.查询所有相同姓氏的人
List findByFamilyList_Username(String username);
}
- 在全局配置文件application.properties中添加Redis数据库连接配置。
spring.data.redis.host=192.268.128.152
spring.data.redis.port=6379
spring.data.redis.password=
注:这里是远程连接linux中的redis,没有设置密码的话,
spring.data.redis.password参数的值为空。
- 编写单元测试进行接口方法测试以及整合测试。
package com.itheima;
import com.itheima.domain.Address;
import com.itheima.domain.Family;
import com.itheima.domain.Person;
import com.itheima.repository.PersonRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@SpringBootTest
class RedisTest {
@Autowired
private PersonRepository personRepository;
@Test
public void savePerson(){
Person person = new Person();
person.setFirstname("三三");
person.setLastname("张");
Address address = new Address();
address.setCity("北京");
person.setAddress(address);
Family family1 = new Family();
family1.setType("父亲");
family1.setUsername("张二");
Family family2 = new Family();
family2.setType("母亲");
family2.setUsername("李一");
ArrayList families = new ArrayList<>();
families.add(family1);
families.add(family2);
person.setFamilyList(families);
// 保存数据
personRepository.save(person);
}
}
- redis储存数据展示如下图。