4.5 Builder API
The Builder API uses LambdaTemplate to describe INSERT, UPDATE, DELETE, QUERY and other operations through chained methods. The matching dialect generates SQL or target commands for execution through a JDBC driver. This is one application adapter entry point for shared access patterns across data sources.
For example, the same condition builder pattern can produce SQL through a relational dialect, document commands through the MongoDB dialect, or Query DSL through an Elasticsearch dialect. Supported operations and conditions are listed in the builder capabilities by data source.
Best For
- You want to reduce handwritten SQL or data source-specific commands.
- Query conditions have many combinations and you don't want to manually concatenate WHERE clauses.
- You want the same API to adapt across different database dialects.
- You need to express more complex conditions beyond BaseMapper's pre-built CRUD.
Not Best For
- SQL or DSL is already fixed and complex enough that maintaining the commands directly is clearer.
- You need lots of database-specific syntax, window functions, or complex JOINs.
- You want SQL/DSL fully maintained by DBAs or in standalone XML files.
For those scenarios, use JdbcTemplate or File Mapper.
Three Modes
| Mode | Requires object mapping | Best for |
|---|---|---|
| Entity mode | Yes | Entity classes exist; use method references like User::getName to reference columns. |
| Map mode | Yes | Table mapping exists, but the data carrier is Map. |
| Freedom mode | No | No entity mapping; directly uses table names and string column names. |
Minimal Example
With DataSource
DataSource dataSource = ...;
LambdaTemplate lambda = new LambdaTemplate(dataSource);
List<User> users = lambda.query(User.class)
.eq(User::getStatus, "ACTIVE")
.ge(User::getAge, 18)
.queryForList();
With Connection
Connection conn = ...;
LambdaTemplate lambda = new LambdaTemplate(conn);
List<User> users = lambda.query(User.class)
.eq(User::getStatus, "ACTIVE")
.ge(User::getAge, 18)
.queryForList();
Freedom mode
Map<String, Object> row = lambda.queryFreedom("users")
.eq("id", 1)
.queryForObject();
Learn More
- LambdaTemplate: complete guide to the Builder API.
- Condition Builder: WHERE condition construction capabilities.
- Object Mapping: table and column mapping that Entity mode depends on.