2021-06-16
2021/6/16 10:53:20
本文主要是介绍2021-06-16,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Spring Boot整合Redis
(一)搭建Redis环境
1、下载Redis安装压缩包
2、将Redis安装包解压到任意位置
3、给Redis配置环境变量
4、启动Redis服务器
5、启动Redis客户端
启动命令行窗口,键入redis-cli命令
创建键值对,按键获取值
(二)安装Redis可视化管理工具
下载链接:https://pan.baidu.com/s/1EaCKpkGr04QM8QHu3H-G_g
提取码:oonx
Redis可视化工具连接Redis服务
(三)创建Spring Boot项目RedisDemo
添加项目依赖
(四)创建实体类
在net.user.lesson08包里创建bean子包
1、创建地址实体类 - Address
在net.user.lesson08.bean包里创建地址实体类Address
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 + '\'' + '}'; } }
注意:索引类Indexed不要导入错误 - import org.springframework.data.redis.core.index.Indexed;
2、创建家庭实体类 - Family
在net.user.lesson08.bean包里创建家庭实体类Family
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 + '\'' + '}'; } }
3、创建个人实体类 - Person
在net.user.lesson08.bean包里创建个人实体类Family
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") 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 + '}'; } }
(五)创建仓库接口 - PersonRepository
在net.user.lesson08包里创建repository子包
在net.user.lesson08.repository包里创建PersonRepository接口
import net.hw.lesson08.bean.Person; import org.springframework.data.repository.CrudRepository; public interface PersonRepository extends CrudRepository<Person, String> { }
(六)在全局配置文件配置Redis属性
(七)在测试类里编写测试方法
点开测试类RedisDemoApplicationTests
注入PersonRepository实例
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条记录~"); }
运行测试方法,查看结果
这篇关于2021-06-16的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-27消息中间件底层原理资料详解
- 2024-11-27RocketMQ底层原理资料详解:新手入门教程
- 2024-11-27MQ底层原理资料详解:新手入门教程
- 2024-11-27MQ项目开发资料入门教程
- 2024-11-27RocketMQ源码资料详解:新手入门教程
- 2024-11-27本地多文件上传简易教程
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器