Spring Data R2DBC (original) (raw)

Last Updated : 30 Mar, 2026

Spring Data R2DBC is a Spring Data module that enables reactive, non-blocking database access for relational databases using Mono and Flux. Unlike JDBC or JPA, it avoids blocking calls, making applications more scalable and high-performing.

Prerequisites

Dependencies

**Maven Dependencies:

XML `

org.springframework.data spring-data-r2dbc 3.1.3
<dependency>
    <groupId>io.r2dbc</groupId>
    <artifactId>r2dbc-postgresql</artifactId>
    <version>0.8.13.RELEASE</version>
</dependency>

<!-- Spring Boot Starter for Web (Optional, if building a web application) -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>3.1.3</version>
</dependency>

<!-- Other dependencies your application may need -->

`

**Gradle Dependencies:

XML `

dependencies { // Spring Boot Starter for R2DBC implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'

// R2DBC driver for your chosen database
implementation 'io.r2dbc:r2dbc-postgresql' // Example for PostgreSQL

// Spring Boot Starter for Web (Optional, if building a web application)
implementation 'org.springframework.boot:spring-boot-starter-web'

// Other dependencies your application may need

}

`

Basic steps to connect a database to a Spring Data R2DBC Application

**1. Add R2DBC driver dependency in pom.xml. For example for Postgres:

XML `

io.r2dbc r2dbc-postgresql

`

**2. Define database configuration properties in application.properties:

Java `

spring.r2dbc.url=r2dbc:postgresql://localhost:5432/database spring.r2dbc.username=username spring.r2dbc.password=password

`

**3. Autowire ConnectionFactory in a configuration class:

Java `

@Configuration public class R2dbcConfig {

@Autowired ConnectionFactory connectionFactory;

}

`

**4. Define entity classes and repository interfaces:

**5. Autowire repository in a service class:

Java `

@Service public class UserService {

private UserRepository userRepository;

public UserService(UserRepository userRepository) { this.userRepository = userRepository; }

}

`

**6. Run the application. Spring Data R2DBC will autoconfigure the connection:

**7. Invoke repository methods from service classes to perform CRUD operations:

Step-by-Step Implementation

Step 1: Create Spring Boot Project

Step 2: Add Dependencies

**Maven

XML `

<!-- Spring Data R2DBC -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>

<!-- R2DBC PostgreSQL Driver -->
<dependency>
    <groupId>io.r2dbc</groupId>
    <artifactId>r2dbc-postgresql</artifactId>
</dependency>

<!-- Optional: Lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

`

**Gradle

Java `

implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc' implementation 'io.r2dbc:r2dbc-postgresql'

`

Step 3: Configure Database

spring.r2dbc.url=r2dbc:postgresql://localhost:5432/testdb
spring.r2dbc.username=postgres
spring.r2dbc.password=admin

Step 4: Create Entity Class

**Employee.java

Java `

import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table;

@Table("employee") public class Employee {

@Id
private Integer id;
private String name;
private Double salary;

// Getters & Setters

}

`

Step 5: Create Repository Interface

import org.springframework.data.repository.reactive.ReactiveCrudRepository; import reactor.core.publisher.Flux;

public interface EmployeeRepository extends ReactiveCrudRepository<Employee, Integer> {

Flux<Employee> findByName(String name);

}

`

Step 6: Create Service Class

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono;

@Service public class EmployeeService {

@Autowired
private EmployeeRepository repository;

public Mono<Employee> save(Employee emp) {
    return repository.save(emp);
}

public Flux<Employee> getAll() {
    return repository.findAll();
}

public Mono<Employee> getById(int id) {
    return repository.findById(id);
}

public Flux<Employee> getByName(String name) {
    return repository.findByName(name);
}

}

`

Step 7: Create Controller

@RestController @RequestMapping("/employees") public class EmployeeController {

@Autowired
private EmployeeService service;

@PostMapping
public Mono<Employee> save(@RequestBody Employee emp) {
    return service.save(emp);
}

@GetMapping
public Flux<Employee> getAll() {
    return service.getAll();
}

}

`

Step 8: Run Application