Spring Boot学习笔记(十一)

Redis介绍

Redis简介

Redis是一个开源(BSD)许可的、内存中的数据结构存储系统,它可以用作数据库缓存消息中间件,并提供多种语言的API。
Redis优点

  1. 存取速度快
  2. 支持丰富的数据类型
  3. 操作具有原子性:确保如果两个客户端并发访问,Redis服务器能接受更新后的值。
  4. 提供多种功能

Redis下载安装
下载地址:https://github.com/MicrosoftArchive/redis/releases
在这里插入图片描述
在这里插入图片描述
下载解压完成之后,打开redis-server
在这里插入图片描述
出现以下图片,Redis服务启动效果
在这里插入图片描述
打开redis-cli可对redis进行操作
在这里插入图片描述
在这里插入图片描述
由于是命令行操作,有些不方便,可以使用Redis可视化管理工具Redis Desktop Manganer(百度下载)
在这里插入图片描述
打开之后的页面:
在这里插入图片描述

打开Redis连接配置,并配置:
在这里插入图片描述
在这里插入图片描述
配置成功:
在这里插入图片描述

使用Spring Boot整合Redis

整合步骤:

  1. 在pom文件中添加Spring Data Redis依赖启动器
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  1. 编写实体类
    Person实体类
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")   //在Redis开一个名为“persons”的存储空间,用于相关操作的存储
public class Person {
    @Id //用于标识实体类主键
    private String id;
    @Indexed    //用于标识该属性会在Redis数据库中生成二级索引
    private String firstname;
    @Indexed
    private String lastname;
    private Address address;	//生成实体类
    private List<Family> familyList;	//生成实体类
    //省略get/set/toString方法
}

Address实体类

import org.springframework.data.redis.core.index.Indexed;

public class Address {
    @Indexed
    private String city;
    @Indexed
    private String country;
    //省略get/set/toString方法
}

Family实体类

import org.springframework.data.redis.core.index.Indexed;

public class Family {
    @Indexed
    private String type;
    @Indexed
    private String username;
    //省略get/set/toString方法
}

  1. 编写Repository接口
    在这里插入图片描述
import com.itheima.chapter03.domain.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> {
    List<Person> findByLastname(String lastname);
    Page<Person> findPersonByLastname(String lastname, Pageable page);
    List<Person> findByFirstnameAndLastname(String firstname,String lastname);
    List<Person> findByAddress_City(String city);
    List<Person> findByFamilyList_Username(String username);
}
  1. 在全局配置文件application.properties中添加Redis数据库连接配置
    在这里插入图片描述
#配置Redis连接
#redis服务器地址
spring.redis.host=127.0.0.1
#redis连接端口
spring.redis.port=6379
#redis服务连接密码
spring.redis.password=
  1. 编写单元测试进行接口方式测试以及整合测试
import com.itheima.chapter03.domain.Address;
import com.itheima.chapter03.domain.Family;
import com.itheima.chapter03.domain.Person;
import com.itheima.chapter03.respoitory.PersonRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;


@SpringBootTest
public class RedisTests{
    @Autowired
    private PersonRepository personRepository;
    @Test
    public void savePerson(){
        Person person = new Person();
        person.setFirstname("三");
        person.setLastname("张");

        Address address = new Address();
        address.setCity("广州");

        person.setAddress(address);

        Family family = new Family();
        family.setType("父亲");
        family.setUsername("张二");

        Family family2 = new Family();
        family.setType("母亲");
        family.setUsername("李芳");

        ArrayList<Family> families = new ArrayList<>();
        families.add(family);
        families.add(family2);

        person.setFamilyList(families);

        personRepository.save(person);

    }
}

测试结果:
打开Redis Desktop Manager,刷新一下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值