Using Redis with CrudRepository in Spring Boot

Posted By : Avnish Pandey | 30-May-2018

Redis Implementation with CrudRepository::
Redis can be used with RedisTemplate and save the data in the key-value pair, but in this blog, I will talk about how to use Redis with CrudRepository means we can write the queries for the result in Redis like JPA with Spring Boot. Spring has provided the Redis library for implementing Redis. For this implementation, we need follow some steps.

First, we need to add the dependency in pom.xml :

<dependency>
	<groupId> org.springframework.boot </groupId>
	<artifactId> spring-boot-starter-data-redis </artifactId>
</dependency>

Then we need to create a domain like a User and a Role :

package com.redis.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;

@RedisHash("user")
public class User {
	private @Id String id;
	private @Indexed String firstName;
	private @Indexed String middleName;
	private @Indexed String lastName;
	private Role role;

	public String getId() {
		return id;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getMiddleName() {
		return middleName;
	}
	public String getLastName() {
		return lastName;
	}
	public Role getRole() {
		return role;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public void setRole(Role role) {
		this.role = role;
	}
}

package com.redis.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.index.Indexed;

public class Role {

	private @Id String id;
	private @Indexed String roleName;

	public String getId() {
		return id;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
}

Now we will create a repository which extends the CrudRepository class for Redis :

package com.redis.repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

import com.redis.domain.User;

public interface UserRepository extends CrudRepository<User, String> {

	List<User> findByFirstNameAndLastName(String firstName, String lastName);

	List<User> findByMiddleNameContains(String firstName);

	List<User> findByRole_RoleName(String roleName);
}

Now create a controller and perform the operations on Redis :

package com.redis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.redis.domain.User;
import com.redis.repository.UserRepository;

@RestController
public class UserController {

	@Autowired
	private UserRepository userRepository;

	@PostMapping("/save_user")
	public User saveUser(@RequestBody User user) {
		return userRepository.save(user);
	}

	@GetMapping("/get_user")
	public List<User> getUser(@RequestParam String firstName, @RequestParam String lastName) {
		return userRepository.findByFirstNameAndLastName(firstName, lastName);
	}
}

Thanks,

About Author

Author Image
Avnish Pandey

Avnish has a good knowledge in core & advance Java, Spring and Hibernate Framework. He loves to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..