Skip to content

Commit 2d28315

Browse files
feat: Add JdbcClient tutorial module (#444)
* feat: add jdbc-client tutorial submodule - Added new `jdbc-client` module demonstrating `JdbcClient` fluent API - Configured Testcontainers and integration tests with `@JdbcTest` - Included `README.adoc` explaining the implementation - Registered the submodule in `settings.gradle.kts` and root `README.adoc` - Preserved existing Spring Boot 4.1.1 and Java 25 ecosystem conventions --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent cb16fc2 commit 2d28315

13 files changed

Lines changed: 311 additions & 0 deletions

File tree

‎README.adoc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ All tutorials are documented in AsciiDoc format and published as an https://anto
6060
|link:data-rest-composite-id[Spring Data REST: Composite Id] |Expose entities with composite ID using Spring Data REST
6161
|link:data-rest-validation[Spring Data REST: Custom Validation] |Implement custom validation for Spring Data REST endpoints
6262
|link:graphql[Spring GraphQL] |Getting started with Spring GraphQL
63+
|link:jdbc-client[Spring JDBC: JdbcClient] |Perform database operations using the fluent JdbcClient API
6364
|link:jooq[jOOQ] |Typesafe SQL with jOOQ and Spring Boot
6465
|link:messaging-kafka[Spring for Apache Kafka] |Implement message publishing and consumption using Spring for Apache Kafka and Testcontainers
6566
|link:messaging-rabbitmq-dlq[Spring AMQP: Dead Letter Queue with RabbitMQ] |Route messages that fail processing to a Dead Letter Queue using Spring AMQP and RabbitMQ

‎jdbc-client/.gitattributes‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/gradlew text eol=lf
2+
*.bat text eol=crlf
3+
*.jar binary

‎jdbc-client/.gitignore‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**/build/
6+
!**/src/test/**/build/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
bin/
17+
!**/src/main/**/bin/
18+
!**/src/test/**/bin/
19+
20+
### IntelliJ IDEA ###
21+
.idea
22+
*.iws
23+
*.iml
24+
*.ipr
25+
out/
26+
!**/src/main/**/out/
27+
!**/src/test/**/out/
28+
29+
### NetBeans ###
30+
/nbproject/private/
31+
/nbbuild/
32+
/dist/
33+
/nbdist/
34+
/.nb-gradle/
35+
36+
### VS Code ###
37+
.vscode/

‎jdbc-client/README.adoc‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
= Spring JDBC: JdbcClient
2+
Rashidi Zin <rashidi@zin.my>
3+
1.0, October 17, 2026: Initial version
4+
:url-repo: https://github.com/rashidi/spring-boot-tutorials/tree/master/jdbc-client
5+
:source-main: {url-repo}/src/main/java/zin/rashidi/boot/jdbc/client
6+
:source-test: {url-repo}/src/test/java/zin/rashidi/boot/jdbc/client
7+
8+
Starting from Spring Boot 3.2, `JdbcClient` is available as a modern and fluent API for database operations,
9+
providing an alternative to the traditional `JdbcTemplate`. It offers a more readable and concise way to interact
10+
with databases while retaining the robust foundation of JDBC.
11+
12+
In this tutorial, we will explore how to perform CRUD operations using `JdbcClient` and verify its behavior using
13+
integration tests powered by Testcontainers.
14+
15+
== Using JdbcClient
16+
17+
First, we will define a simple `User` record to represent our domain model.
18+
19+
[source,java]
20+
----
21+
public record User(Long id, String name, String username, String email) {
22+
}
23+
----
24+
25+
Next, let's create our repository. We will use `JdbcClient` to implement `findAll`, `findById`, and `save` operations.
26+
27+
[source,java]
28+
----
29+
@Repository
30+
public class UserRepository {
31+
32+
private final JdbcClient jdbcClient;
33+
34+
public UserRepository(JdbcClient jdbcClient) {
35+
this.jdbcClient = jdbcClient;
36+
}
37+
38+
public List<User> findAll() {
39+
return jdbcClient.sql("SELECT id, name, username, email FROM users")
40+
.query(User.class)
41+
.list();
42+
}
43+
44+
public Optional<User> findById(Long id) {
45+
return jdbcClient.sql("SELECT id, name, username, email FROM users WHERE id = :id")
46+
.param("id", id)
47+
.query(User.class)
48+
.optional();
49+
}
50+
51+
public void save(User user) {
52+
jdbcClient.sql("INSERT INTO users (id, name, username, email) VALUES (:id, :name, :username, :email)")
53+
.param("id", user.id())
54+
.param("name", user.name())
55+
.param("username", user.username())
56+
.param("email", user.email())
57+
.update();
58+
}
59+
}
60+
----
61+
62+
As seen in the snippet above, the syntax of `JdbcClient` makes query binding and result mapping highly readable. We use the `.param()` method for named parameters, and mapping a row to our `User` record is effortlessly handled by `.query(User.class)`.
63+
64+
== Verifying with Testcontainers
65+
66+
Now we will test our repository against a real PostgreSQL database, ensuring all queries are working correctly.
67+
68+
[source,java]
69+
----
70+
@DataJdbcTest
71+
@Testcontainers
72+
@Import(UserRepository.class)
73+
class UserRepositoryTests {
74+
75+
@Container
76+
@ServiceConnection
77+
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
78+
79+
@Autowired
80+
private UserRepository repository;
81+
82+
private static final List<User> USERS = List.of(
83+
new User(1L, "Rashidi Zin", "rashidi.zin", "rashidi@zin.my"),
84+
new User(2L, "Zaid Zin", "zaid.zin", "zaid@zin.my")
85+
);
86+
87+
@BeforeEach
88+
void setUp() {
89+
USERS.forEach(repository::save);
90+
}
91+
92+
@Test
93+
void findAll_ShouldReturnAllUsers() {
94+
List<User> users = repository.findAll();
95+
assertThat(users).hasSize(2)
96+
.containsExactlyInAnyOrderElementsOf(USERS);
97+
}
98+
99+
@Test
100+
void findById_ShouldReturnUser_WhenIdExists() {
101+
Optional<User> user = repository.findById(1L);
102+
assertThat(user).isPresent().contains(USERS.get(0));
103+
}
104+
105+
@Test
106+
void findById_ShouldReturnEmpty_WhenIdDoesNotExist() {
107+
Optional<User> user = repository.findById(999L);
108+
assertThat(user).isEmpty();
109+
}
110+
}
111+
----
112+
113+
Using `@DataJdbcTest`, Spring auto-configures a testing environment dedicated to JDBC-based applications. Combined with `@ServiceConnection` from Spring Boot, connecting to Testcontainers becomes trivial, requiring no explicit jdbc property configurations.
114+
115+
The full implementation of this tutorial can be found on link:{url-repo}[GitHub].

‎jdbc-client/build.gradle.kts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
plugins {
2+
java
3+
id("org.springframework.boot") version "4.1.1"
4+
id("io.spring.dependency-management") version "1.1.7"
5+
}
6+
7+
group = "zin.rashidi.boot"
8+
version = "0.0.1-SNAPSHOT"
9+
10+
java {
11+
toolchain {
12+
languageVersion = JavaLanguageVersion.of(25)
13+
}
14+
}
15+
16+
repositories {
17+
mavenCentral()
18+
}
19+
20+
dependencies {
21+
implementation("org.springframework.boot:spring-boot-starter-jdbc")
22+
runtimeOnly("org.postgresql:postgresql")
23+
testImplementation("org.springframework.boot:spring-boot-starter-data-jdbc-test")
24+
testImplementation("org.springframework.boot:spring-boot-testcontainers")
25+
testImplementation("org.testcontainers:testcontainers-junit-jupiter")
26+
testImplementation("org.testcontainers:testcontainers-postgresql")
27+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
28+
}
29+
30+
tasks.withType<Test> {
31+
useJUnitPlatform()
32+
}

‎jdbc-client/settings.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = "jdbc-client"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package zin.rashidi.boot.jdbc.client;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class JdbcClientApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(JdbcClientApplication.class, args);
11+
}
12+
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package zin.rashidi.boot.jdbc.client;
2+
3+
public record User(Long id, String name, String username, String email) {
4+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package zin.rashidi.boot.jdbc.client;
2+
3+
import org.springframework.jdbc.core.simple.JdbcClient;
4+
import org.springframework.stereotype.Repository;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
@Repository
10+
public class UserRepository {
11+
12+
private final JdbcClient jdbcClient;
13+
14+
public UserRepository(JdbcClient jdbcClient) {
15+
this.jdbcClient = jdbcClient;
16+
}
17+
18+
public List<User> findAll() {
19+
return jdbcClient.sql("SELECT id, name, username, email FROM users")
20+
.query(User.class)
21+
.list();
22+
}
23+
24+
public Optional<User> findById(Long id) {
25+
return jdbcClient.sql("SELECT id, name, username, email FROM users WHERE id = :id")
26+
.param("id", id)
27+
.query(User.class)
28+
.optional();
29+
}
30+
31+
public void save(User user) {
32+
jdbcClient.sql("INSERT INTO users (id, name, username, email) VALUES (:id, :name, :username, :email)")
33+
.param("id", user.id())
34+
.param("name", user.name())
35+
.param("username", user.username())
36+
.param("email", user.email())
37+
.update();
38+
}
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.sql.init.mode=always

0 commit comments

Comments
 (0)