Skip to content

Commit 3a94423

Browse files
committed
Comparion of JDBC Builders
1 parent 4d6ed20 commit 3a94423

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

content/docs/why/sql-builders.md

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,50 @@ weight: 2
44
toc: true
55
---
66

7-
**SQL Builders** are tools designed to simplify database interaction by abstracting away complex SQL queries and connection management. They help developers focus on writing business logic rather than managing database connections and handling raw SQL queries. Popular SQL Builders like **Spring JDBC** and **QueryDSL** allow developers to construct SQL queries programmatically, making code more readable and maintainable.
7+
**SQL Builders** are tools designed to simplify database interaction by abstracting away complex SQL queries and connection management. They help developers focus on writing business logic rather than handling raw SQL queries manually. Modern tools like **Spring Boot 3 JdbcClient** allow developers to construct and execute SQL queries programmatically with a fluent, readable API.
88

99
Out of the three main responsibilities—connection management, model/DTO creation, and DAO implementation—SQL Builders primarily remove the burden of **connection management** from developers.
1010

11-
For example, when using **Spring JDBC Client**, developers still need to create:
11+
When using the **Spring Boot 3 JdbcClient**, developers are still responsible for creating:
1212

1313
- **Model classes**
14-
- **Data Access Objects (DAO)** using JDBC Clients
15-
- **RowMappers** to map database results to Java objects
14+
- **Data Access Objects (DAO)** using the fluent Client API
15+
- **Row Mapping logic** (though `JdbcClient` simplifies this with `query().row(...)`)
1616

17-
Here’s a sample of how this works with **Spring JDBC Client**:
17+
Here is a sample of how this works using the **Spring Boot 3 JdbcClient**:
1818

1919
```java
2020
// Model class
21-
public class Movie {
22-
private Long id;
23-
private String title;
24-
private String directedBy;
25-
// Getters and Setters
26-
}
21+
public record Movie(Long id, String title, String directedBy) {}
2722

28-
// DAO using Spring JDBC
23+
// DAO using Spring Boot 3 JdbcClient
24+
@Repository
2925
public class MovieRepository {
3026

31-
private JdbcTemplate jdbcTemplate;
27+
private final JdbcClient jdbcClient;
3228

33-
public MovieRepository(JdbcTemplate jdbcTemplate) {
34-
this.jdbcTemplate = jdbcTemplate;
29+
public MovieRepository(JdbcClient jdbcClient) {
30+
this.jdbcClient = jdbcClient;
3531
}
3632

3733
public List<Movie> findMoviesByDirector(String director) {
38-
String sql = "SELECT * FROM movie WHERE directed_by = ?";
39-
return jdbcTemplate.query(sql, new Object[]{director}, new RowMapper<Movie>() {
40-
public Movie mapRow(ResultSet rs, int rowNum) throws SQLException {
41-
Movie movie = new Movie();
42-
movie.setId(rs.getLong("id"));
43-
movie.setTitle(rs.getString("title"));
44-
movie.setDirectedBy(rs.getString("directed_by"));
45-
return movie;
46-
}
47-
});
34+
return jdbcClient.sql("SELECT id, title, directed_by FROM movie WHERE directed_by = :director")
35+
.param("director", director)
36+
.query((rs, rowNum) -> new Movie(
37+
rs.getLong("id"),
38+
rs.getString("title"),
39+
rs.getString("directed_by")
40+
))
41+
.list();
4842
}
4943
}
5044
```
5145

5246
### SQL Components: A Type-Safe Alternative
5347

54-
While SQL Builders abstract connection management, **SQL Components** offers additional benefits by providing a **type-safe API** and eliminating the need for manual code related to models, DAOs, and RowMappers. Here's how you can achieve the same functionality using SQL Components:
48+
While `JdbcClient` offers a much cleaner fluent API than the older `JdbcTemplate`, **SQL Components** goes further by providing a **fully type-safe API** and eliminating the need to write any manual code for models, DAOs, or RowMappers.
49+
50+
Here is how you achieve the same functionality using SQL Components:
5551

5652
```java
5753
List<Movie> movies = DataManager.getManager().getMovieStore()
@@ -62,6 +58,6 @@ List<Movie> movies = DataManager.getManager().getMovieStore()
6258

6359
**Key Differences**:
6460

65-
1. **Automatic Code Generation**: Models, DAOs, and RowMappers are generated automatically.
66-
2. **Type-Safe API**: Compile-time error checking ensures safety and reduces runtime issues.
67-
3. **Framework Independence**: No need for external frameworks like Spring; it’s pure Java code that can be easily integrated into any Java application.
61+
1. **Zero Boilerplate**: Models, Store classes, and mapping logic are generated automatically from your schema.
62+
2. **True Type-Safety**: Queries are constructed using generated metadata, ensuring that column names and types are checked at compile-time.
63+
3. **Framework Independence**: Operates as pure Java code. While it works perfectly with Spring, it doesn't require it, making your persistence layer portable across any Java environment.

0 commit comments

Comments
 (0)