Master SpringBoot 3.2 JdbcClient: Fluent API for Simple Database Operations

This article introduces SpringBoot 3.2's JdbcClient, explains how to add the dependency, inject it, and demonstrates fluent‑API query and insert operations—including positional, named, map‑based parameters and custom RowMapper—showing how to perform CRUD without heavyweight ORM frameworks.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master SpringBoot 3.2 JdbcClient: Fluent API for Simple Database Operations

SpringBoot 3.2 introduces JdbcClient, a lightweight fluent‑API wrapper for database access that complements JdbcTemplate, Spring Data JDBC and Spring Data JPA.

Overview

JdbcClient is a lightweight database operation framework with a fluent API, easy to read and maintain, and supports complex SQL.

Adding JdbcClient

First add the spring‑data‑jdbc starter dependency.

implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'

Inject JdbcClient into a service.

@Component
public class DbService {
    @Autowired
    private JdbcClient jdbcClient;
}

Query Operations

JdbcClient can query by primary key or custom conditions.

Query by primary key:

public MyData findDataById(Long id) {
    return jdbcClient.sql("select * from my_data where id = ?")
                     .params(id)
                     .query(MyData.class)
                     .single();
}

Query by custom condition:

public List<MyData> findDataByName(String name) {
    return jdbcClient.sql("select * from my_data where name = ?")
                     .params(name)
                     .query(MyData.class)
                     .list();
}

Named‑parameter query:

public Integer insertDataWithNamedParam(MyData myData) {
    Integer rowsAffected = jdbcClient.sql("insert into my_data values(:id,:name)")
                                    .param("id", myData.id())
                                    .param("name", myData.name())
                                    .update();
    return rowsAffected;
}

Map‑based parameter query:

public List<MyData> findDataByParamMap(Map<String, ?> paramMap) {
    return jdbcClient.sql("select * from my_data where name = :name")
                     .params(paramMap)
                     .query(MyData.class)
                     .list();
}

RowMapper for complex result mapping:

public List<MyData> findDataWithRowMapper() {
    return jdbcClient.sql("select * from my_data")
                     .query((rs, rowNum) -> new MyData(rs.getLong("id"), rs.getString("name")))
                     .list();
}

Count query example:

public Integer countByName(String name) {
    return jdbcClient.sql("select count(*) from my_data where name = ?")
                     .params(name)
                     .query(Integer.class)
                     .single();
}

Insert Operations

Insert using positional parameters:

public Integer insertDataWithParam(MyData myData) {
    return jdbcClient.sql("insert into my_data values(?,?)")
                     .param(myData.id())
                     .param(myData.name())
                     .update();
}

Insert using named parameters or an object as a parameter source:

public Integer insertDataWithNamedParam(MyData myData) {
    return jdbcClient.sql("insert into my_data values(:id,:name)")
                     .param("id", myData.id())
                     .param("name", myData.name())
                     .update();
}
public Integer insertDataWithObject(MyData myData) {
    return jdbcClient.sql("insert into my_data values(:id,:name)")
                     .paramSource(myData)
                     .update();
}

Summary

JdbcClient enables all basic CRUD operations without the overhead of full‑blown ORM frameworks, offering a simpler and more flexible fluent‑API approach.

Full source code is available at https://github.com/qihaiyan/springcamp/tree/master/spring-data-jdbc-client.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavadatabasespringbootJdbcClientFluent APISpring Data JDBC
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.