2021-05-17

一、使用Spring Boot 整合 Redis
(一)搭建Redis环境
1、下载Redis安装压缩包
下载链接:https://pan.baidu.com/s/1x8AV7w1PE6fR7xS7lwAOWg
提取码:j4hq
2、将Redis安装包解压到任意位置
解压到D:\Program Files\redis目录
3、给Redis配置环境变量
4、启动Redis服务器
在这里插入图片描述
(二)创建Spring Boot项目RedisDemo
(三)创建实体类
1、创建地址实体类Address
在net.tjl.lesson08.bean包里创建地址实体类Address
在这里插入图片描述

package net.tjl.lesson08.bean;

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

/**
 * 功能:地址实体类
 * 作者:谭金兰
 * 日期:2021年05月17日
 */
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 + '\'' +
                '}';
    }
}

2、创建家庭实体类Family
在这里插入图片描述

package net.tjl.lesson08.bean;

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

/**
 * 功能:家庭实体类
 * 作者:谭金兰
 * 日期:2021年05月17日
 */
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 + '\'' +
                '}';
    }
}

3、创建人实体类Person
在这里插入图片描述

package net.tjl.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;

/**
 * 功能:人实体类
 * 作者:谭金兰
 * 日期:2021年05月17日
 */
@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 +
                '}';
    }
}

(四)创建自定义Repository接口
在这里插入图片描述

package net.tjl.lesson08.repository;

import net.tjl.lesson08.bean.Person;
import org.springframework.data.repository.CrudRepository;

/**
 * 功能:人仓库接口
 * 作者:谭金兰
 * 日期:2021年05月17日
 */
public interface PersonRepository extends CrudRepository<Person, String> {
}

(五)在全局配置文件配置Redis属性
在这里插入图片描述
(六)在测试类里编写测试方法
在这里插入图片描述

1、创建测试方法testAddPerson()

@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条记录~");                                                                        
}                                                                                                                                                                                           

运行测试方法,查看结果
在这里插入图片描述
打开Redis可视化工具查看
在这里插入图片描述
2、创建测试方法testFindAll()
在这里插入图片描述
运行测试方法,查看结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值