Spring Boot整合Redis
Posted qq_48838980
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot整合Redis相关的知识,希望对你有一定的参考价值。
文章目录
一、搭建Redis环境
1、下载Redis安装压缩包
- 下载链接:https://pan.baidu.com/s/1x8AV7w1PE6fR7xS7lwAOWg
- 提取码:j4hq
2、将Redis安装包解压到任意位置
3、配置环境变量
4、启动服务器
- 输入:redis-server
5、启动客户端
- 注意:输入redis-cli 不要关闭redis-server运行窗口,才能成功
6、创建键值对,按键获取值
二、安装Redis可视化管理工具
1、下载Redis可视化管理工具
- 下载链接:https://pan.baidu.com/s/1EaCKpkGr04QM8QHu3H-G_g
- 提取码:oonx
2、安装Redis可视化管理工具
4、Redis可视化工具连接Redis服务
三、项目实现
1、创建项目文件
- 添加依赖
2、创建地址实体类
package net.zjs.lesson08.bean;
import org.springframework.data.redis.core.index.Indexed;
/**
* 功能:地址实体类
* 作者:zjs
* 日期: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 + '\\'' +
'}';
}
}
3、创建家庭实体类
package net.zjs.lesson08.bean;
import org.springframework.data.redis.core.index.Indexed;
/**
* 功能:家庭实体类
* 作者:zjs
* 日期: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 + '\\'' +
'}';
}
}
4、创建人实体类
package net.zjs.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;
/**
* 功能:个人实体类
* 作者:zjs
* 日期: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 +
'}';
}
}
5、创建自定义Repository接口
package net.zjs.lesson08.repository;
import net.zjs.lesson08.bean.Person;
import org.springframework.data.repository.CrudRepository;
/**
* 功能:人仓库接口
* 作者:zjs
* 日期:2021-05-17
*/
public interface PersonRepository extends CrudRepository<Person,String> {
}
6、配置全局配置文件
7、编写测试方法
package net.zjs.lesson08;
import net.zjs.lesson08.bean.Address;
import net.zjs.lesson08.bean.Family;
import net.zjs.lesson08.bean.Person;
import net.zjs.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
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条记录~");
}
}
8、运行测试方法
- 查看结果
9、测试findAll()方法
- 运行测试,查看结果
10、测试findById()方法
- 运行测试,查看方法
11、测试自定义个性化查询方法
(1)在PersonRepository接口定义方法
(2)在测试类创建测试方法
- 根据姓查找
- 运行测试方法,查看结果
12、课后作业
任务:在测试类创建测试方法testFindByLastName()
- 运行测试方法,查看结果
任务:在测试类创建测试方法testFindPersonByLastNameAndFirstName()
- 运行测试方法,查看结果
任务:在测试类创建测试方法testFindByAddress_City()
- 运行测试方法,查看结果
任务:在测试类创建测试方法testFindByFamilyList_Name()
- 运行测试方法,查看结果
以上是关于Spring Boot整合Redis的主要内容,如果未能解决你的问题,请参考以下文章