A MyBatis Alternative for Java Order Pages: Generated APIs
If you are looking for a MyBatis alternative for a Java application, start with the work your team repeats: SQL mapping, relation loading, DTO assembly, counts and page-specific filters. This comparison asks when a model-generated query API can reduce that repetition and when direct SQL control is more useful.
MyBatis is a productive and familiar tool for Java teams. It gives developers direct control over SQL and mapping. For many simple services, that is enough.
The pressure appears when a business page needs more than one table and more than one query shape.
Consider an order page.
MyBatis vs generated APIs for an order page
The main tradeoff is who maintains the data-access contract. MyBatis keeps SQL and result mappings under application control. TeaQL generates a typed business API from a model, then uses a runtime/provider to execute the request.
| Order-page requirement | MyBatis approach | Generated TeaQL approach |
|---|---|---|
| Filter and order the parent list | Maintain parameterized statements or a SQL builder. | Compose model-derived predicates and ordering methods. |
| Load an item's preview with each Order | Choose joins, nested result maps, nested selects or a separate batched query. | Select the relation with a bounded child Request. |
| Show the filtered total | Maintain a count statement with the same authorized predicates. | Request a count on the parent query and read the returned total carrier. |
| Return a view-specific shape | Define the result mapping and any application response assembly. | Select a typed graph, then map it to the application's response contract. |
| Change a field or relationship | Review affected statements, mappings and callers. | Re-evaluate the model, regenerate, refresh Assist and compile affected callers. |
| Enforce buyer/tenant visibility | Apply trusted authorization in the service and database access boundary. | Install and review the trusted context/policy boundary; a generated predicate alone is insufficient. |
MyBatis already supports reusable SQL fragments and nested result mappings. It does not require every relationship to be stitched together manually. The comparison concerns repeated application-owned mapping work versus adopting a generated model contract. Neither a fluent call chain nor one mapper invocation establishes the number of SQL statements, query performance or an N+1 guarantee.
When is a MyBatis alternative worth evaluating?
| Your application | What to evaluate |
|---|---|
| A few clear, tuned SQL statements | Keeping mapper-based access may be the simpler choice. |
| Many pages repeat the same domain relations, filters and counts | Compare a generated business API with the mapper/DTO assembly you maintain today. |
| A migration with existing mapper XML and integrations | Budget for model adoption and integration work; this is not a drop-in MyBatis configuration change. |
| A strict latency or database-specific requirement | Compare the actual provider SQL and plan on your workload. This article supplies no speed or migration-cost benchmark. |
The bounded-page excerpt below comes from the retained Java Order application. For startup, imports and version qualifications, use the Java Order tutorial. The Java data-access comparison provides a wider code-shape overview; its competitor snippets are illustrative and version-dependent.
The Order Page Shape
A typical order page may need:
- the order list;
- customer name;
- order status;
- line item preview;
- line item count;
- status counts for tabs;
- pagination;
- merchant or tenant filtering;
- permission-aware visibility.
With mapper-oriented persistence, this often becomes several mapper methods, XML fragments, DTO assembly, and post-query stitching.
The MyBatis Shape
A MyBatis implementation might involve:
- one SQL query for the order list;
- one query or join for customer fields;
- one query for line item previews;
- one query for counts;
- one query for grouped status statistics;
- result maps or manual DTO mapping;
- service code to assemble the final response.
That is explicit and controllable, but the business intent is spread across files.
The TeaQL Shape
The retained Order example expresses a bounded parent page, one-item previews
and a filtered total through a generated request. This is a function-body excerpt
from the complete Java application,
with existing buyer, offset, limit and trusted context variables:
var page=Q.customerOrders().withBuyerReferenceIs(buyer).orderByTotalMinorAscending().orderByIdAscending()
.selectOrderItemListWith(Q.orderItems().orderBySkuCodeAscending().orderByIdAscending().limit(1)).offset(offset,limit).count()
.comment("Read bounded Order pages").purpose("Compare filtered totals and per-order item previews").executeForList(context);
The surrounding handler rejects negative offsets and limits outside [1, 3].
page.getTotalCount() returns the matching parent total, while the child bound
returns at most one preview item per Order. Sorting by total then ID gives the
page a deterministic tie-breaker. The buyer predicate is a business filter in
this fixture, not proof of authenticated ownership.
The dated SQLite receipt qualifies this Java core example on the published runtime. Later lifecycle and sustained-load checks retain their separate WORKSPACE PASS — RELEASE PENDING qualification. This excerpt does not implement every status bucket, permission rule or response field listed above; use the Java Order query recipes for the separately explained export, retained-page and summary operations.
What TeaQL Reduces
TeaQL does not eliminate thinking. It reduces repetitive plumbing:
- mapper XML;
- duplicated DTO assembly;
- repeated relation-loading code;
- repeated count/statistics query fragments;
- stringly-typed field references;
- one-off page query services.
The generated model becomes the shared vocabulary.
Where MyBatis Still Wins
MyBatis remains a good choice when:
- a query must be hand-tuned;
- SQL is the clearest expression of the business requirement;
- the domain model is small;
- teams need exact control over vendor-specific SQL;
- generated APIs would add weight without reducing repetition.
TeaQL and MyBatis do not need to be ideological opposites. TeaQL is valuable when the business model is large enough that generated APIs reduce repeated work.
The Review Difference
In a TeaQL code review, the reviewer can ask:
- does this page select the correct relations?
- are the counts grouped correctly?
- is pagination applied at the right level?
- does the runtime context apply tenant and permission policy?
That is a higher-level review than checking XML fragments and DTO stitching.
The Short Version
MyBatis gives direct SQL control. TeaQL gives generated business APIs.
For complex business pages, TeaQL keeps page intent closer to the domain model.
Try one bounded page before planning a migration
Follow the Java Order query tutorial for native paging, counts and summaries, and buyer/tenant isolation for the application-owned authorization boundary. A tenant predicate in an illustrative query is not a substitute for installing and reviewing the full trusted access boundary. Use generated APIs vs handwritten SQL to decide where each query belongs.
