开源
(BSD许可)的、内存中的数据结构
存储系统,它可以用作数据库
、缓存
和消息中间件
,并提供多种语言的API。110000
次的设值操作,或者执行81000
次的读取操作。D:\Program Files\redis
目录redis-server
命令6379
6379
接受连接redis-cli
命令exit
命令,退出客户端
city
是先前利用Redis客户端创建的)package net.hw.lesson08.bean;
import org.springframework.data.redis.core.index.Indexed;
/**
* 功能:地址实体类
* 作者:华卫
* 日期:2021年05月17日
*/
public class Address {
@Indexed
private String country; //国家
@Indexed
private String city; //城市
public Address(String country, String city) {
this.country = country;
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"country='" + country + '\'' +
", city='" + city + '\'' +
'}';
}
}
Indexed
不要导入错误 - import org.springframework.data.redis.core.index.Indexed;
package net.hw.lesson08.bean;
import org.springframework.data.redis.core.index.Indexed;
/**
* 功能:家庭实体类
* 作者:华卫
* 日期:2021年05月17日
*/
public class Family {
@Indexed
private String type; //成员类型
@Indexed
private String name; //成员名
public Family(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Family{" +
"type='" + type + '\'' +
", name='" + name + '\'' +
'}';
}
}
package net.hw.lesson08.bean;
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;
/**
* 功能:个人实体类
* 作者:华卫
* 日期:2021年05月17日
*/
@RedisHash("persons")
public class Person {
@Id //主键
private String id;
//生成二级索引,方便查询
@Indexed
private String firstName; //名
@Indexed
private String lastName; //姓
private Address address; //家庭地址
private List<Family> familyList; //家庭成员
public Person(String id, String firstName, String lastName, Address address, List<Family> familyList) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.familyList = 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<Family> getFamilyList() {
return familyList;
}
public void setFamilyList(List<Family> familyList) {
this.familyList = familyList;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", address=" + address +
", familyList=" + familyList +
'}';
}
}
@RedisHash("persons")
,表明在redis数据库中开启一个persons
的内存空间,所有person
操作相关的数据均保存在此空间(redis是内存数据库)。package net.hw.lesson08.repository;
import net.hw.lesson08.bean.Person;
import org.springframework.data.repository.CrudRepository;
/**
* 功能:个人仓库接口
* 作者:华卫
* 日期:2021年05月17日
*/
public interface PersonRepository extends CrudRepository<Person, String> {
}
@Test
public void testAddPerson() {
// 添加第一个人
Address address = new Address("中国", "泸州");
Family family1 = new Family("儿子", "张晓刚");
Family family2 = new Family("女儿", "张晓霞");
List<Family> familyList = new ArrayList<Family>();
familyList.add(family1);
familyList.add(family2);
Person person = new Person("1", "无忌", "张", address, familyList);
personRepository.save(person);
// 添加第二个人
address = new Address("中国", "上海");
family1 = new Family("儿子", "李功晨");
family2 = new Family("女儿", "李晓丽");
familyList = new ArrayList<Family>();
familyList.add(family1);
familyList.add(family2);
person = new Person("2", "承鹏", "李", address, familyList);
personRepository.save(person);
// 添加第三个人
address = new Address("中国", "北京");
family1 = new Family("儿子", "唐玉海");
family2 = new Family("女儿", "唐雨涵");
familyList = new ArrayList<Family>();
familyList.add(family1);
familyList.add(family2);
person = new Person("3", "大明", "唐", address, familyList);
personRepository.save(person);
// 添加第四个人
address = new Address("中国", "北京");
family1 = new Family("儿子", "张大明");
family2 = new Family("女儿", "张丽丽");
familyList = new ArrayList<Family>();
familyList.add(family1);
familyList.add(family2);
person = new Person("4", "文勇", "张", address, familyList);
personRepository.save(person);
System.out.println("成功地添加了4条记录~");
}
package net.hw.lesson08.repository;
import net.hw.lesson08.bean.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
/**
* 功能:人仓库接口
* 作者:华卫
* 日期:2021年05月17日
*/
public interface PersonRepository extends CrudRepository<Person, String> {
//自定义个性化查询,方法名需要符合特定的规范
List<Person> findByLastName(String lastName);
Page<Person> findPersonByLastName(String lastName, Pageable pageable);
List<Person> findPersonByLastNameAndFirstName(String lastName, String firstName);
List<Person> findByAddress_City(String city);
List<Person> findByFamilyList_Name(String name);
}