|
| 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]. |
0 commit comments