Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

A team with complex reporting SQL is choosing between MyBatis, JdbcClient and jOOQ for a new service. Compare them for type safety, dynamic SQL, testing and long-term maintenance.

All three give full control of SQL, unlike JPA, but they differ in where safety comes from. MyBatis keeps SQL in XML or annotations with mature dynamic tags and result mapping, but SQL errors only show at runtime; JdbcClient is part of Spring and needs no extra dependency, but dynamic SQL is string building by hand; jOOQ generates Java code from the schema so SQL is checked at compile time and dynamic queries are typed, with a commercial licence for some databases. For complex reporting with many dynamic filters, jOOQ is usually the strongest, but the choice should follow the team's skills, and one service should use one of them.

Why this question is asked

Reporting services are where ORMs struggle. Queries join many tables, use window functions and grouping, and change shape depending on dozens of filters. The team wants full SQL, but also wants the code to stay safe and readable for years. Interviewers ask this comparison because it tests whether we can weigh trade-offs rather than repeat a favourite library. In this answer, we will recap what each option is, write the same dynamic query in all three, compare them for type safety, dynamic SQL, testing and maintenance, and finish with how to choose.

1A Quick Recap of the Three Options

MyBatis is a SQL mapper. We write SQL in XML files or annotations, with tags such as <if>, <where> and <foreach> for dynamic parts, and MyBatis binds parameters and maps rows to objects through result maps. It has been around for a long time and is very popular in some regions and in many legacy enterprise systems.

JdbcClient arrived in Spring Framework 6.1 as a fluent front end over JdbcTemplate. It is part of Spring itself, so there is nothing to add. We write SQL as strings, bind named or positional parameters, and map rows with a RowMapper or straight to a record.

jOOQ takes a different approach. A code generator reads our database schema, for example after Flyway migrations have run, and generates Java classes for every table and column. We then write SQL as a Java DSL, a set of methods that read like SQL, and the compiler checks table names, column names and types.

2The Same Dynamic Query Three Ways

Our example is a sales report with two optional filters: region and a minimum total. First with JdbcClient, where we build the SQL ourselves.

Java
// JdbcClient: dynamic SQL built by hand
StringBuilder sql = new StringBuilder("SELECT region, sum(total) AS total FROM sales WHERE day >= :from");
Map<String, Object> params = new HashMap<>(Map.of("from", from));
if (region != null) { sql.append(" AND region = :region"); params.put("region", region); }
sql.append(" GROUP BY region");
if (minTotal != null) { sql.append(" HAVING sum(total) >= :min"); params.put("min", minTotal); }
List<RegionTotal> rows = jdbc.sql(sql.toString()).params(params).query(RegionTotal.class).list();
// MyBatis XML: dynamic tags
<select id="regionTotals" resultType="RegionTotal">
SELECT region, sum(total) AS total FROM sales
<where> day &gt;= #{from} <if test="region != null">AND region = #{region}</if></where>
GROUP BY region
<if test="minTotal != null">HAVING sum(total) &gt;= #{minTotal}</if>
</select>
// jOOQ: typed DSL from generated classes
Condition where = SALES.DAY.ge(from);
if (region != null) where = where.and(SALES.REGION.eq(region));
var q = ctx.select(SALES.REGION, sum(SALES.TOTAL).as("total")).from(SALES).where(where).groupBy(SALES.REGION);
List<RegionTotal> rows = (minTotal == null ? q : q.having(sum(SALES.TOTAL).ge(minTotal)))
.fetchInto(RegionTotal.class);

Notice how each one handles the optional parts. JdbcClient needs care with spaces, the order of clauses and keeping the parameter map in sync with the text. MyBatis expresses the same logic declaratively, but in XML, where even >= must be escaped. jOOQ composes conditions as objects, so there is no string assembly at all, and a misspelled column such as SALES.REGOIN does not compile.

3Type Safety: When Do We Find the Mistake?

The most important difference is when an error appears. In MyBatis and JdbcClient, SQL is text. A renamed column, a wrong type or a typo is found when the statement runs, ideally in an integration test, otherwise in production. MyBatis can at least validate that the XML is well formed and the statement ids match the interface at startup, but not that the SQL is right.

With jOOQ, the generated classes are a copy of the schema. When a migration renames a column and we regenerate, every query that used the old name stops compiling, and the compiler lists them. For a long-lived reporting service with hundreds of queries and a changing schema, that is a large safety gain. The cost is a code generation step in the build, usually run against a Testcontainers database after migrations.

Comparing the options for a reporting service
ConcernMyBatisJdbcClientjOOQ
SQL errors foundAt runtimeAt runtimeMostly at compile time
Dynamic SQLMature XML tagsHand-built stringsTyped, composable conditions
Result mappingRich result maps, nestingRowMapper or recordsRecords, POJOs, nested collections
Extra dependencyStarter and XML filesNone, part of SpringLibrary and code generator
LicenceOpen sourceOpen sourceOpen source for open-source databases; commercial for some others
Check jOOQ's current licence terms for your database.

4Dynamic SQL and Complex Reports

For a report with three filters, any of the three is fine. For a report with twenty optional filters, optional joins and user-selected grouping, the difference grows. MyBatis handles it well, and teams that know it are productive, though large XML files with nested conditions become hard to read and refactor. JdbcClient is the weakest here: hand-built SQL strings with many branches are easy to get wrong and are where SQL injection creeps in, when someone appends a value instead of a parameter.

jOOQ is strongest for this shape of problem. Conditions, joins and even selected columns are Java values that we can build in loops and helper methods, test separately and reuse. It also supports advanced SQL, window functions, common table expressions and vendor-specific features, with types checked.

5Testing and Long-Term Maintenance

For all three, the important tests run against the real database with Testcontainers, using the production version. They give full SQL control, which means the database is part of what we test. None of them needs mocks of the database layer to be useful; mocking a SQL mapper only proves we called it.

For maintenance, the questions are who will read the code in five years and how often the schema changes. MyBatis spreads logic across Java and XML, which some teams like and others find harder to navigate. JdbcClient keeps everything in Java with no extra concepts, ideal for a small number of straightforward queries. jOOQ adds a build step and a licence question for some commercial databases, but makes schema changes the safest of the three. Mixing all three in one service doubles the knowledge needed and gives no benefit, so the service should pick one main approach.

6How to Choose

I would decide by three factors: the team's existing skills, how dynamic the queries are, and how much schema-driven safety is worth. A team experienced with MyBatis, on an open-source database, with moderately dynamic reports, can choose MyBatis and be productive immediately. A service with a handful of fixed queries should simply use JdbcClient and avoid a dependency. A reporting service with many dynamic queries over an evolving schema is where jOOQ pays for its setup cost.

Step throughChoosing a SQL library for the service
  1. Only a few fixed queries?
    YesJdbcClientNoContinue
  2. Many dynamic filters over a changing schema?
    YesjOOQ (check the licence for your database)NoContinue
  3. Team already strong in MyBatis?
    YesMyBatisNojOOQ or JdbcClient, by preference
  4. Record the decision and use one approach per service

4 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Can jOOQ and JPA live in the same service?

Yes, and it is a fairly common combination: JPA for simple entity writes and jOOQ for complex reads and reports, both on the same DataSource and the same Spring transaction. That is different from mixing three query libraries for the same kind of work, because each one has a clear, separate role.

Is JdbcClient safe from SQL injection?

It is as safe as the SQL we write. Values passed with param() or params() are bound as prepared statement parameters and are safe. The risk comes from building the SQL string with user input, for example appending a sort column directly. With dynamic SQL built by hand, every appended piece must be a constant or come from a whitelist.

8Conclusion

In this answer, we compared MyBatis, JdbcClient and jOOQ for a reporting service. All three give full SQL control and should be tested against the real database with Testcontainers. MyBatis offers mature dynamic tags and result mapping with runtime SQL errors, JdbcClient is built into Spring with no extra dependency but leaves dynamic SQL to hand-built strings, and jOOQ generates code from the schema for compile-time checks and a typed DSL, with a commercial licence for some databases. We choose by team skill, how dynamic the queries are and the value of schema-driven safety, and use one approach per service.

What to say in the interviewAbout 90 seconds aloud

Open with

All three keep us in control of the SQL, which is why they suit reporting better than JPA, so the comparison is about where safety comes from. MyBatis keeps SQL in XML or annotations with dynamic tags like where, if and foreach, and has mature result mapping, including nested objects.

Then, if they want more

It is very productive for teams that know it, but SQL is text, so typos and renamed columns show up at runtime. JdbcClient is part of Spring 6.1 and later, so there is no extra dependency, and it is great for a handful of clear queries with records as results.

For heavy dynamic SQL, though, we build strings by hand, which is error prone and is where injection risks appear. jOOQ generates Java classes from the schema, so queries are checked by the compiler, a migration that renames a column breaks the build instead of production, and dynamic conditions are composed as typed objects.

It needs a code generation step, and some commercial databases need a paid licence. In every case I would test against the real database with Testcontainers. For a reporting service with complex, dynamic SQL and an evolving schema, I would lean to jOOQ; if the team is strong in MyBatis, that is a fine choice too. What I would avoid is mixing all three in one service.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
java.sql, package documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A team with complex reporting SQL is choosing between MyBatis, JdbcClient and jOOQ for a...": https://scrivio-interview-library.netlify.app/databases/jdbc/767d748e35/