介紹 Spring Data Redis
本文介紹介紹 Spring Data Redis,對當前炙手可熱的數據結構存儲引擎Redis數據庫提供了Spring Data訪問抽象。
Redis是基於key存儲數據結構的數據庫,可用於持久化數據、緩存以及消息代理等。
我們可以使用通用的Spring Data模型(如template),對傳統spring data項目的簡化。
必備依賴
gradle 依賴(基於spring boot):
compile 'redis.clients:jedis:2.9.0'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
maven 依賴:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
</dependency>
讀者可以查找相應最新版本。
redis 配置
首先定義應用和redis服務實例之間連接,需要使用Redis client。針對java有很多Redis client實現,本文我們使用jedis————簡單且強大的客戶端實現。我們採用java config方式配置,當然也可以使用xml方式進行配置。
java config方式配置
Configuration bean 定義:
@Configuration
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisCfg= new RedisStandaloneConfiguration();
redisCfg.setHostName("192.168.0.102");
redisCfg.setPort(6379);
redisCfg.setPassword("123456"); // 訪問密碼
return new JedisConnectionFactory(redisCfg);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
}
配置非常簡單。首先使用RedisStandaloneConfiguration,然後定義JedisConnectionFactory。接着定義RedisTemplate,repository使用其操作redis數據。
Redis Repository
定義Student實體:
package com.dataz.redis.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.redis.core.RedisHash;
import java.io.Serializable;
@RedisHash("Student")
@Setter
@Getter
@AllArgsConstructor
@ToString
public class Student implements Serializable {
public enum Gender {
/**
* male
*/
MALE,
/**
* female
*/
FEMALE
}
private String id;
private String name;
private Gender gender;
private int grade;
}
Spring Data Repository
定義StudentRepository 接口:
package com.dataz.redis.db;
import com.dataz.redis.entity.Student;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends CrudRepository<Student, String> {}
使用StudentRepository操作數據
通過繼承CrudRepository ,可以獲得完整的持久化方法實現CRUD功能。
保存對象
創建實體並保存至redis:
Student student = new Student(
"Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.save(student);
查詢存在的對象
可以通過查詢驗證前一步是否成功:
Student retrievedStudent =
studentRepository.findById("Eng2015001").get();
更新存在的對象
修改之前返回對象的名稱:
retrievedStudent.setName("Richard Watson");
studentRepository.save(student);
可以再次驗證是否修改成功。
刪除對象
刪除上面插入的數據:
studentRepository.deleteById(student.getId());
可以查詢對象並驗證是否爲null。
返回所有對象
首先插入幾個對象實例:
Student engStudent = new Student(
"Eng2015001", "John Doe", Student.Gender.MALE, 1);
Student medStudent = new Student(
"Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
studentRepository.save(engStudent);
studentRepository.save(medStudent);
也可以使用saveAll方法實現插入對象集合,其接收Iterable類型包括多個持久化對象。
可以通過findAll方法查找所有對象:
List<Student> students = new ArrayList<>();
studentRepository.findAll().forEach(students::add);
那麼我們可以檢查集合大小進行驗證。更細粒度檢查可以通過便利檢查每個對象屬性。
總結
本文簡單介紹了JRedis,通過spring data方式操作redis數據。