课堂学习 Day06 |Spring Boot基础-Spring Boot整合Redis

本文档详细介绍了如何在SpringBoot项目中集成并使用Redis,包括环境搭建、实体类和仓库接口创建、配置文件设置,以及添加、查找、删除和自定义查询等操作的测试。同时,提供了测试代码及运行效果展示。
摘要由CSDN通过智能技术生成

写在前面的话

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()(测试自定义个性化查询方法)
在这里插入图片描述

三、补充

1、Redis官网Redis官网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值