一、数据库连接
1、使用redis可视化软件连接redis数据库
二、创建RedisDemo项目
1、创建Spring Boot项目RedisDemo并且完成项目初始化工作
2、在net.hy.lesson08包里创建bean子包,然后创建实体类Address
package net.hy.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 + '\'' +
'}';
}
}
3、创建家庭实体类 - Family和个人实体类-Person
Family实体类 -
package net.hy.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 + '\'' +
'}';
}
}
Person实体类
package net.hy.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 +
'}';
}
}
4、创建仓库接口 - PersonRepository
5、在application.properties文件里配置Redis属性
6、在测试类RedisDemoApplicationTests编写测试方法testAddPerson()
@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条记录~");
}
运行结果