Spring Boot学习8——Redis

redis

  • redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

优点
(1) 速度快:因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1)
(2) 支持丰富数据类型:支持string,list,set,sorted set,hash
(3) 支持事务:操作都是原子性,所谓的原子性就是对数据的更改要么全部执行,要么全部不执行
(4) 丰富的特性:可用于缓存,消息,按key设置过期时间,过期后将会自动删除

搭建Redis环境

1.下载Redis压缩包并解压

https://github.com/microsoftarchive/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.zip

2.配置Redis环境变量

在这里插入图片描述

3.启动redis

在这里插入图片描述

  • 启动客户端之前需要先启动服务端,服务端也可以用cmd窗口打开,cmd窗口输入redis-cli,使用exit命令退出客户端
    在这里插入图片描述
    在这里插入图片描述

4.下载Redis可视化管理工具(Redis Desktop Manager)

链接:https://pan.baidu.com/s/1vIIEWciNXyZDHRgPobGGMg
提取码:1234
在这里插入图片描述

  • 先启动服务端再创建redis连接
    在这里插入图片描述

使用Spring Boot整合Redis

一、创建项目RedisDemo

在这里插入图片描述
在这里插入图片描述

二、创建家庭实体类Family

  • 创建bean子包
    在这里插入图片描述
package net.lj.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 + '\'' +
                '}';
    }
}

三、创建地址实体类Address

在这里插入图片描述

package net.lj.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 + '\'' +
                '}';
    }
}

四、创建人实体类Person

在这里插入图片描述

package net.lj.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") //redis数据库中开启一个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 +
                '}';
    }
}

五、创建自定义Repository接口

  • 创建Repository子包
    在这里插入图片描述
package net.lj.lesson08.repository;

import net.lj.lesson08.bean.Person;
import org.springframework.data.repository.CrudRepository;
/**
 * 人仓库接口
 */
public interface PersonRepository extends CrudRepository<Person, String> {
}

六、全局配置文件配置Redis属性

在这里插入图片描述

七、编写测试方法

package net.lj.lesson08;

import net.lj.lesson08.bean.Address;
import net.lj.lesson08.bean.Family;
import net.lj.lesson08.bean.Person;
import net.lj.lesson08.repository.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;
import java.util.List;

@SpringBootTest
class RedisDemoApplicationTests {

    //注入个人仓库
    @Autowired
    private PersonRepository personRepository;

    @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<Person> persons = personRepository.findAll();
        persons.forEach(((p) -> System.out.println(p)));
    }
     @Test
    public void testFindById(){
        Optional<Person> person = personRepository.findById("2");
        System.out.println(person);
    }
}

启动测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值