Six Powerful Spring Boot JDBC Data Access Techniques

This article introduces six Spring Boot approaches for JDBC data access—including JdbcTemplate, NamedParameterJdbcTemplate, JdbcClient, SimpleJdbcInsert/Call, Spring Data JDBC repositories, and JdbcAggregateTemplate—each illustrated with code samples and usage notes for Spring Boot 3.5.0.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Six Powerful Spring Boot JDBC Data Access Techniques

Java Database Connectivity (JDBC) is the standard API for Java applications to interact with relational databases. The article demonstrates six ways to perform JDBC operations within Spring Boot 3.5.0.

1. JdbcTemplate

JdbcTemplate simplifies resource management and reduces boilerplate by handling connection creation, statement execution, and result extraction. The developer only provides the SQL and maps rows to objects.

private final JdbcTemplate jdbcTemplate;

public List<Book> findAll() {
  String query = "SELECT * FROM t_book";
  return jdbcTemplate.query(query, (res, rowNum) -> Book.builder()
      .id(res.getLong("id"))
      .title(res.getString("title"))
      .isbn(res.getString("isbn"))
      .description(res.getString("description"))
      .page(res.getInt("page"))
      .price(res.getBigDecimal("price"))
      .build());
}

2. NamedParameterJdbcTemplate

This variant supports named parameters, making SQL statements more readable compared with the traditional '?' placeholders. It delegates most work to an underlying JdbcTemplate.

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public List<Book> findByTitle(String title) {
  String query = "SELECT * FROM t_book WHERE title like :title";
  Map<String, Object> params = new HashMap<>();
  params.put("title", "%" + title + "%");
  return namedParameterJdbcTemplate.query(query, params, (res, rowNum) -> Book.builder()
      .id(res.getLong("id"))
      .title(res.getString("title"))
      .isbn(res.getString("isbn"))
      .description(res.getString("description"))
      .page(res.getInt("page"))
      .price(res.getBigDecimal("price"))
      .build());
}

3. JdbcClient (Spring 6.1+)

Introduced in Spring 6.1, JdbcClient offers a fluent, chain‑style API that supports both positional and named parameters and acts as a unified façade for PreparedStatement operations.

private final JdbcClient jdbcClient;

public List<Book> queryBooks() {
  String query = "SELECT * FROM t_book";
  return jdbcClient.sql(query)
      .query(Book.class)
      .list();
}

Further usage details are linked in the original article.

4. SimpleJdbcInsert and SimpleJdbcCall

Both classes leverage database metadata to simplify insert statements and stored‑procedure calls.

SimpleJdbcInsert provides a reusable, thread‑safe way to perform batch inserts by supplying the table name and a map of column values.

private final SimpleJdbcInsert simpleJdbcInsert;

public BookService(DataSource dataSource) {
  this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource)
      .withCatalogName("ddd")
      .withTableName("t_book")
      .usingGeneratedKeyColumns("id");
}

public Long insertBook(Book book) {
  MapSqlParameterSource parameters = new MapSqlParameterSource();
  parameters.addValue("title", book.getTitle());
  parameters.addValue("isbn", book.getIsbn());
  parameters.addValue("description", book.getDescription());
  parameters.addValue("page", book.getPage());
  parameters.addValue("price", book.getPrice());
  return simpleJdbcInsert.executeAndReturnKey(parameters).longValue();
}

SimpleJdbcCall encapsulates calls to stored procedures or functions, also using metadata to generate the required SQL.

private final SimpleJdbcCall simpleJdbcCall;

public BookService(DataSource dataSource) {
  this.simpleJdbcCall = new SimpleJdbcCall(dataSource)
      .withCatalogName("ddd");
}

public Map<String, Object> getBookById(Long bookId) {
  Map<String, Object> params = new HashMap<>();
  params.put("bookId", bookId);
  simpleJdbcCall.withProcedureName("getBookById");
  return simpleJdbcCall.execute(params);
}

5. Jdbc Repositories (Spring Data JDBC)

By adding the spring-boot-starter-data-jdbc dependency, developers can define repository interfaces that extend Spring Data abstractions such as PagingAndSortingRepository.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

@Table(name = "t_book")
public class Book {
  @Id
  private Long id;
  // ... other fields and getters/setters
}
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
  // additional query methods can be declared here
}
public Page<Book> findBooks() {
  return bookRepository.findAll(PageRequest.of(0, 20));
}

6. JdbcAggregateTemplate

As an alternative to repositories, JdbcAggregateTemplate provides a more direct way to load and persist aggregate roots.

private final JdbcAggregateTemplate jdbcAggTemplate;

public Book queryBook(Long id) {
  return this.jdbcAggTemplate.findById(id, Book.class);
}

The template offers many additional methods (shown in the accompanying image) for CRUD operations, batch updates, and query execution.

Overall, the article presents six concrete Spring‑Boot‑based techniques for JDBC data access, each with code snippets and brief explanations, enabling developers to choose the most suitable approach for their project.

Jdbc methods overview
Jdbc methods overview
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.

Spring BootJDBCJdbcTemplateJdbcClientSimpleJdbcInsertJdbcAggregateTemplateNamedParameterJdbcTemplateSimpleJdbcCall
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

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.