You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/why/sql-builders.md
+25-29Lines changed: 25 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,54 +4,50 @@ weight: 2
4
4
toc: true
5
5
---
6
6
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.
8
8
9
9
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.
10
10
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:
12
12
13
13
-**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(...)`)
16
16
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**:
18
18
19
19
```java
20
20
// Model class
21
-
publicclassMovie {
22
-
privateLong id;
23
-
privateString title;
24
-
privateString directedBy;
25
-
// Getters and Setters
26
-
}
21
+
public record Movie(Long id, String title, String directedBy) {}
return jdbcClient.sql("SELECT id, title, directed_by FROM movie WHERE directed_by = :director")
35
+
.param("director", director)
36
+
.query((rs, rowNum) ->newMovie(
37
+
rs.getLong("id"),
38
+
rs.getString("title"),
39
+
rs.getString("directed_by")
40
+
))
41
+
.list();
48
42
}
49
43
}
50
44
```
51
45
52
46
### SQL Components: A Type-Safe Alternative
53
47
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:
55
51
56
52
```java
57
53
List<Movie> movies =DataManager.getManager().getMovieStore()
@@ -62,6 +58,6 @@ List<Movie> movies = DataManager.getManager().getMovieStore()
62
58
63
59
**Key Differences**:
64
60
65
-
1.**Automatic Code Generation**: Models, DAOs, and RowMappers are generated automatically.
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