Redis简介:
Redis 是一个开源(BSD许可)的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件,并提供多种语言的API。
Redis优点:
- 存取速度快:Redis速度非常快,每秒可执行大约110000次的设值操作,或者执行81000次的读取操作。
- 支持丰富的数据类型:Redis支持开发人员常用的大多数数据类型,例如列表、集合、排序集和散列等。
- 操作具有原子性:所有Redis操作都是原子操作,这确保如果两个客户端并发访问,Redis服务器能接收更新后的值。
- 提供多种功能:Redis提供了多种功能特性,可用作非关系型数据库、缓存中间件、消息中间件等。
下载:
- Linux:https://redis.io/download 或 https://download.redis.io/releases/
- Windows:https://github.com/MicrosoftArchive/redis/tags
- 下载安装教程可参考:https://blog.csdn.net/leisure_life/article/details/82078233
整合步骤:
-
在pom文件中添加Spring Data Redis依赖启动器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
编写实体类
@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 class Address { @Indexed private String city; @Indexed private String country; }
-
编写Repository接口
public interface PersonRepository extends CrudRepository<Person, String> { List<Person> findByAddress_City(String city); }
-
在全局配置文件application.properties中添加Redis数据库连接配置
spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=
-
编写单元测试进行接口方法测试以及整合测试
@RunWith(SpringRunner.class) @SpringBootTest public class RedisTests { @Autowired private PersonRepository repository; @Test public void selectPerson() { List<Person> list = repository.findByAddress_City("北京"); System.out.println(list); } }
文章评论