写在前面的话
1、参考学习自:https://blog.csdn.net/howard2005/article/details/107972598
2、内容如果有不对的,希望可以指出或补充。
3、课堂回顾。
一、概述
Redis:是一个基于内存的高性能key-value数据库。其优点
为:存取速度快、数据类型丰富、操作具有原子性(确保如果两个客户端并发访问,Redis服务器能接收更新后的值)、提供多种功能。
二、操作
(一)效果实现
1、搭建Redis环境、安装Redis可视化管理工具略,具体查看【参考学习链接内容】。
2、创建Spring Boot项目RedisDemo
其中需要注意的部分如下。
3、创建实体类、仓库接口
Address(地址实体类)、Family(家庭实体类)、Person(个人实体类)略,具体查看【参考学习链接内容】。
PersonRepository(仓库接口)
4、application.properties(全局配置文件)
(二)测试
1、测试方法
RedisdemoApplicationTests.java
package net.ch.lesson08.redisdemo;
import net.ch.lesson08.redisdemo.bean.Address;
import net.ch.lesson08.redisdemo.bean.Family;
import net.ch.lesson08.redisdemo.bean.Person;
import net.ch.lesson08.redisdemo.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 contextLoads() {
}
@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条记录~");
}
@Test
public void testFindAll(){
//Iterable:可迭代的,可重复的
Iterable<Person> persons = personRepository.findAll();
persons.forEach(person -> System.out.println(person));
}
@Test
public void testFindById(){
Optional<Person> person = personRepository.findById("2");
System.out.println(person);
}
@Test
public void testDeleteAll(){
personRepository.deleteAll();
System.out.println("删除成功!");
}
@Test
//测试自定义个性化查询-姓
public 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);
}
}
}
2、测试效果
testAddPerson()
testFindById()
testFindPersonByLastName()(测试自定义个性化查询方法)