文章目录
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);
}
}
启动测试