这个也是数据库没了,直接上代码吧
package net.yq.lesson08.bean;
import org.springframework.data.redis.core.index.Indexed;
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 + '\'' +
'}';
}
}
package net.yq.lesson08.bean;
import org.springframework.data.redis.core.index.Indexed;
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.yq.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;
@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 +
'}';
}
}
package net.yq.lesson08.repository;
import net.yq.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;
public interface PersonRepository extends CrudRepository<Person,String> {
public List<Person> findByLastName(String lastName);
public Page<Person> findPersonByLastName(String lastName, Pageable pageable);
public List<Person> findPersonByLastNameAndFirstName(String lastName,String firstName);
List<Person> findByAddress_City(String city);
List<Person> findByFamilyList_Name(String name);
}
package net.yq.lesson08;
import net.yq.lesson08.bean.Address;
import net.yq.lesson08.bean.Family;
import net.yq.lesson08.bean.Person;
import net.yq.lesson08.repository.PersonRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@SpringBootTest
class RedisDemoApplicationTests {
@Autowired
private PersonRepository personRepository;
@Test
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条记录~");
}
@Test
void testFindAll(){
Iterable<Person> persons = personRepository.findAll();
persons.forEach(System.out::println);
}
@Test
void testFindById(){
Optional<Person> person = personRepository.findById("1");
System.out.println(person.get());
}
@Test
void testDeleteAll(){
personRepository.deleteAll();
}
//测试自定义接口
@Test
void testFindPersonByLastName(){
Sort.Direction sort = Sort.Direction.DESC;
Pageable pageable = PageRequest.of(0,2,sort,"id");
Page<Person> page = personRepository.findPersonByLastName("赵",pageable);
for (Person person : page.getContent()){
System.out.println(person);
}
}
@Test
void testFindByLastName(){
List<Person> person = personRepository.findByLastName("赵");
System.out.println(person);
}
@Test
void testFindPersonByLastNameAndFirstName(){
List<Person> person = personRepository.findPersonByLastNameAndFirstName("赵", "零零");
System.out.println(person);
}
@Test
void testFindByAddress_City(){
List<Person> person = personRepository.findByAddress_City("斗罗大陆");
System.out.println(person);
}
@Test
void testFindByFamilyList_Name(){
List<Person> person = personRepository.findByFamilyList_Name("四四");
System.out.println(person);
}
}
结束!