介绍 Spring Data Redis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/neweastsun/article/details/87195182

介绍 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数据。

猜你喜欢

转载自blog.csdn.net/neweastsun/article/details/87195182