DEV Community

ts5432
ts5432

Posted on

Create condition using JooqTemplate

1. condition

Create a Condition from a Field, an operator, and a value.

public Condition condition(Name fieldName, String operator, Object[] values)
Enter fullscreen mode Exit fullscreen mode

2. conditions

Parse varargs into List. Arguments are grouped in pairs (fieldName, value). An operator suffix can be appended to the field name.

public List<Condition> conditions(Object... condtionKvs)
Enter fullscreen mode Exit fullscreen mode

example:

// Basic usage: pairs of (fieldName, value)
List<Condition> conds = jt.conditions(
    "name", "John",                 // name = 'John'
    "birthday≥", beginDate        // birthday ≥ '2020-01-01'
);

// Mixing Condition objects
List<Condition> conds = jt.conditions(
    "name", name,
    F("create_time").isNotNull(),  // passing a Condition directly
    "birthday:desc"               // OrderBy: birthday DESC (ignored for conditions)
);

// Using conditions with query
List<User> list = jt.query("user_table", User.class,
    jt.conditions("name%", name, "birthday>=", beginDate));
Enter fullscreen mode Exit fullscreen mode
Operator Meaning Example
(none) or == Equal (eq / isNull) "name", value → name = ?
% Contains (LIKE) "name%", "Jo" → name LIKE '%Jo%'
%L Starts with "name%L", "Jo" → name LIKE 'Jo%'
%R Ends with "name%R", "hn" → name LIKE '%hn'
> Greater than "birthday>", date → birthday > ?
>= Greater than or equal "birthday>=", date → birthday >= ?
< Less than "age<", 18 → age < ?
<= Less than or equal "age<=", 18 → age <= ?
<> Not equal "name<>", "admin" → name <> ?
in IN query "id", list (Collection/array value) → id IN (?,?,?)
notin NOT IN query "id:notin", list → id NOT IN (?,?,?)
isnull IS NULL (unary, no value argument) "name:isnull" → name IS NULL
isnotnull IS NOT NULL (unary, no value argument) "name:isnotnull" → name IS NOT NULL

3. Order By

In varargs, can append sort operators to field names to specify ordering. can also pass OrderField objects directly.
example:

// Mix ordering in varargs
List<User> list = jt.queryv("user_table", User.class,
    "birthday:desc",            // ORDER BY birthday DESC
    F("name").asc().nullsLast()  // ORDER BY name ASC NULLS LAST
);

// Pass order list to query()
List<User> list = jt.query("user_table", User.class, conditions,
    Arrays.asList(F("birthday").desc(), F("name").asc()));
Enter fullscreen mode Exit fullscreen mode
Sort Operator Meaning
fieldName:asc ASC
fieldName:desc DESC
fieldName:ascnf ASC NULLS FIRST
fieldName:ascnl ASC NULLS LAST
fieldName:descnf DESC NULLS FIRST
fieldName:descnl DESC NULLS LAST

Top comments (0)