DEV Community

ts5432
ts5432

Posted on

Query records using JooqTemplate

1. query

Query multiple records matching the condition and map them to a list of entities.

// Single condition
public <E> List<E> query(String table, Class<E> cls, Condition condition)
public <E> List<E> query(String table, Class<E> cls, Condition condition, List<? extends OrderField> orderBy)
// Multiple conditions
public <E> List<E> query(String table, Class<E> cls, List<Condition> conditions)
public <E> List<E> query(String table, Class<E> cls, List<Condition> conditions, List<? extends OrderField> orderBy)
// Using RecordMapper
public <R,E> List<E> query(String table, RecordMapper<R,E> mapper, Condition condition)
public <R,E> List<E> query(String table, RecordMapper<R,E> mapper, List<Condition> conditions)
Enter fullscreen mode Exit fullscreen mode

Returns: List — list of entities, never null.
Example:

// Single condition
List<User> list = jt.query("user_table", User.class,
    F("name").contains("John"));

// Multiple conditions + order
List<User> list = jt.query("user_table", User.class,
    jt.conditions("name%", name, "birthday>=", beginDate),
    Arrays.asList(F("birthday").desc(), F("name").asc()));

// Using RecordMapper
List<User> list = jt.query("user_table", new MyMapper(),
    F("id").gt(0));
Enter fullscreen mode Exit fullscreen mode

2. queryRecord

public Result<Record> queryRecord(String table, Condition condition)
public Result<Record> queryRecord(String table, List<Condition> conditions, List<? extends OrderField> orderBy)
public Result<Record> queryRecordv(String table, Object... conditions)
Enter fullscreen mode Exit fullscreen mode

Example:

Result<Record> rs = jt.queryRecordv("user_table", "name%", "Jo");
for(Record r : rs) {
    System.out.println(r.get("name"));
}
Enter fullscreen mode Exit fullscreen mode

3. queryLowerCaseRecord

public List<LowerCaseRecord> queryLowerCaseRecord(String table, Condition condition)
public List<LowerCaseRecord> queryLowerCaseRecordv(String table, Object... conditions)
Enter fullscreen mode Exit fullscreen mode

4. queryv (varargs conditions)

Query a list using varargs conditions. Combine conditions and ordering in a single line.

public <E> List<E> queryv(String table, Class<E> cls, Object... conditions)
public <E> List<E> queryv(TableLike table, Class<E> cls, Object... conditions)
Enter fullscreen mode Exit fullscreen mode

Example:

// Fuzzy + range query
List<User> list = jt.queryv("user_table", User.class,
    "name%", name, "birthday>=", beginDate, "birthday<=", endDate);

// Mixed ordering
List<User> list = jt.queryv("user_table", User.class,
    "birthday:asc", F("name").asc().nullsLast());
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.