DEV Community

ts5432
ts5432

Posted on

Query Single Record with SelectList In JooqTemplate

Using JooqTemplate requires no code generation and no XML configuration.

Add Dependency

gradle:

implementation 'net.sf.sprtool:sprtool-jooq:1.7.0'
Enter fullscreen mode Exit fullscreen mode

maven:

<dependency>
    <groupId>net.sf.sprtool</groupId>
    <artifactId>sprtool-jooq</artifactId>
    <version>1.7.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Inject JooqTemplate

import static net.sf.sprtool.jooq.JooqFields.*;

import net.sf.sprtool.jooq.JooqTemplate;

@Autowired
private JooqTemplate jt;
Enter fullscreen mode Exit fullscreen mode

Using JooqTemplate

TableList and SelectList

  • JooqFields.TL(...) builds a TableList: accepts multiple tables and supports aliases, e.g. TL("user_table u", "dept_table d").
  • JooqFields.SL(...) builds a SelectList: specifies the columns to query and supports the "tableAlias.field outputAlias" syntax, where the output alias maps automatically to the Bean property (d.name dept_name → deptName).

v Methods (Recommended, More Concise)

The v-suffixed loadv / loadRecordv / loadLowerCaseRecordv pass all conditions and ordering directly as varargs — the most concise style. The following sections describe the equivalent Condition-list style for side-by-side reference:

TableList tl = JooqFields.TL("order o", "user u");
SelectList sl = JooqFields.SL("o.id", "o.order_no", "o.amount", "u.name user_name");

// Conditions + ordering all as varargs: u.name = 'John', ordered by o.id ascending
OrderView order = jt.loadv(tl, OrderView.class, sl, "u.name", "John", "o.id:asc");

// Record / LowerCaseRecord versions
Record record = jt.loadRecordv(tl, sl, "u.name", "John", "o.id:asc");
LowerCaseRecord lr = jt.loadLowerCaseRecordv(tl, sl, "u.name", "John");

// Mix in a jOOQ Condition (join conditions are built with F(...))
OrderView r = jt.loadv(tl, OrderView.class, sl,
        "u.name", "John",
        JooqFields.F("o", "user_id").eq(JooqFields.F("u", "id")));
Enter fullscreen mode Exit fullscreen mode

Single-Table Query with SelectList

TableList tableList = JooqFields.TL("product");
SelectList selectList = JooqFields.SL("id", "name", "price", "category_id");

Condition condition = DSL.field("name").eq("Smartphone");

Product product = jt.load(tableList, Product.class, selectList, Arrays.asList(condition));
Enter fullscreen mode Exit fullscreen mode

When fetching a single field, receive it directly as a String / Integer:

String name = jt.load(tableList, String.class, JooqFields.SL("name"), Arrays.asList(condition));
Enter fullscreen mode Exit fullscreen mode

Multi-Table Join Query

Pass multiple aliased tables in the TableList and qualify fields with their aliases in the conditions to perform a JOIN query (mirroring the test example that joins user_table with dept_table to fetch the department name):

TableList tableList = JooqFields.TL("order o", "user u");
SelectList selectList = JooqFields.SL("o.id", "o.order_no", "o.amount", "u.name user_name");

Condition joinCond = DSL.field("o.user_id").eq(DSL.field("u.id"));
Condition nameCond = DSL.field("u.name").eq("John");

OrderView order = jt.load(tableList, OrderView.class, selectList,
        Arrays.asList(joinCond, nameCond));
// order.getUserName() is mapped from "u.name user_name"
Enter fullscreen mode Exit fullscreen mode

Condition List + Ordering

The non-v variants accept a List<Condition> together with ordering (the ...ConditionOrderBy / ...ConditionsOrderBy overloads):

List<Condition> conditions = Arrays.asList(
        DSL.field("category_id").eq(2),
        DSL.field("name").like("%Lee%"));

Product product = jt.load(tableList, Product.class, selectList, conditions);
Enter fullscreen mode Exit fullscreen mode

loadv: Varargs Style

loadv(TableList, Class, SelectList, fieldValuePairs...) passes conditions and ordering as varargs, with operators embedded in field names; jOOQ Condition objects can be mixed in:

// dept_id = 2 and name like '%Lee%', ordered by id ascending
UserEntity result = jt.loadv(tableList, UserEntity.class, selectList,
        "dept_id", 2, "name%", "Lee", "id:asc");

// No conditions at all: returns the first record
UserEntity first = jt.loadv(tableList, UserEntity.class, selectList);

// Mix in a jOOQ Condition (join condition)
UserEntity r = jt.loadv(tl, UserEntity.class, sl,
        "u.name", "John",
        JooqFields.F("u", "dept_id").eq(JooqFields.F("d", "id")),
        "d.name", "Finance Department");
Enter fullscreen mode Exit fullscreen mode

loadRecordv / loadLowerCaseRecordv

The v variants that return a Record / LowerCaseRecord — no Class argument:

Record record = jt.loadRecordv(tableList, selectList, "name", "Smartphone", "id:asc");
String name = record.get("name", String.class);

LowerCaseRecord lr = jt.loadLowerCaseRecordv(tableList, selectList, "name", "Smartphone");
Object price = lr.get("price");
Enter fullscreen mode Exit fullscreen mode

Behavior Notes

  • Returns null when no record matches.
  • Empty varargs do not throw; they are equivalent to an unconditional query and return the first record.
  • The % operator means a contains-match (name% + "John" → name like '%John%').
  • Output aliases in the SelectList (d.name dept_name) map automatically to the Bean's camelCase properties.

Top comments (0)