I introduced you to Spring Data R2DBC. With Spring Boot, configuring and using Spring Data R2DBC is much simpler and more convenient. How is it in detail? Let’s find out together in this tutorial!
First, I will create a Spring Boot project with Spring Data R2DBC Starter and PostgreSQL dependencies as an example.
The result is as follows:
With Spring Boot, to configure the ConnectionFactory object, you just need to use some properties starting with “spring.r2dbc” as follows:
1 2 3 |
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/spring_data_r2dbc_example spring.r2dbc.username=khanh spring.r2dbc.password=1 |
You can also configure the connection pool using the following properties:
1 2 3 |
spring.r2dbc.pool.enabled=true spring.r2dbc.pool.initial-size=3 spring.r2dbc.pool.max-size=10 |
Very simple, right?
For Repository classes, you also define them as usual.
For example, I also have a table “student”:
1 2 3 4 5 |
CREATE TABLE student ( id INT NOT NULL, name VARCHAR(50) DEFAULT NULL, PRIMARY KEY (id) ) |
with the following content in the PostgreSQL database:
The model class for the “student” table, I will define it as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.huongdanjava.springdatar2dbc; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table; @Table public class Student { private static final long serialVersionUID = 1L; @Id private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
The Repository class for the Student model is as follows:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava.springboot; import org.springframework.data.r2dbc.repository.R2dbcRepository; import org.springframework.stereotype.Repository; @Repository public interface StudentRepository extends R2dbcRepository<Student, Long> { } |
At this point, we have finished configuring Spring Data R2DBC with Spring Boot in our application, guys!
Now, if I run Spring Boot with Java console with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.huongdanjava.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootSpringDataR2dbcExampleApplication implements CommandLineRunner { @Autowired StudentRepository studentRepository; public static void main(String[] args) { SpringApplication.run(SpringBootSpringDataR2dbcExampleApplication.class, args); } @Override public void run(String... args) throws Exception { studentRepository.findAll().log() .map(s -> { System.out.println(s.getName()); return s; }) .subscribe(); Thread.sleep(10000); } } |
The result will be as follows: