Spring Data Redis CRUD Operations example with Spring Boot

Redis is an open-source, in-memory key-value data store, used as a database, cache, and message broker. In this tutorial, we’re gonna look at how to do Redis CRUD Operations with Spring Boot using Spring Data Redis.

Related Post: Spring Data Redis Messaging – PubSub example | Spring Boot

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.9.0.RELEASE
– Spring Boot: 1.5.9.RELEASE

II. Spring Data Redis

1. Maven Dependency

2. Redis Configuration

We use Jedis as a Redis client to define and establish the connection between our Spring Boot application and the Redis server instance.

If we wanna configure connection details, just modify the jedisConnectionFactory configuration:

3. Redis CRUD Operations with RedisTemplate

RedisTemplate provides DefaultHashOperations instance that can do hash-related operations for data manipulation.
To get DefaultHashOperations instance, we call RedisTemplate.opsForHash():

III. Practice

1. Project Structure


Customer is a Data Model Class.
CustomerRepository is an interface which will be autowired in WebController for implementing repository methods and custom finder methods.
WebController is a REST Controller which has request mapping methods for RESTful requests such as: save, findall, findbyid, update, delete.
RedisConfig defines and establishes the connection between our Spring Boot application and the Redis server instance.
Dependencies for Spring Boot and Spring Data Redis in pom.xml

2. Step by Step
1. Create Spring Boot project & add Dependencies

2. Configure Spring Data Redis

Under package config, create RedisConfig class:

3. Create DataModel Class

Under package model, create Customer class:

4. Create Repository

This interface helps us do all CRUD functions:

And its implementation:

5. Create Web Controller

The controller receives requests from client, using repository to do CRUD Operations and return results:

6. Run Spring Boot Application & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1
The browser returns Done.

Request 2

Request 3

Request 4.1
The browser returns Done.

>> Request 4.2: check update

Request 5.1
The browser returns Done.

>> Request 5.2: check delete

IV. Source Code


By JavaSampleApproach | December 31, 2017.

Related Posts

2 thoughts on “Spring Data Redis CRUD Operations example with Spring Boot”

Got Something To Say:

Your email address will not be published. Required fields are marked *