JooqTemplate을 사용한 페이지 매김 레코드

작성자

카테고리:

← 피드로
DEV Community · ts5432 · 2026-06-15 개발(SW)

ts5432

Paginated queries with automatic total count calculation. Supports specifying result fields.

public <E> LimitResult<List<E>,E> query(Class<E> cls, LimitSelect limitSelect)
public <E> LimitResult<List<E>,E> query(Class<E> cls, LimitSelect limitSelect, LimitRange range)
public <E> LimitResult<List<E>,E> query(Class<E> cls, LimitSelect limitSelect, List resultFields)
public <E> LimitResult<List<E>,E> query(Class<E> cls, LimitSelect limitSelect, LimitRange range, List resultFields)

Enter fullscreen mode Exit fullscreen mode

Returns: LimitResult — contains getResult() (data list) and getTotal() (total count).
Example:

// Define pagination query
LimitSelect limitSelect = new LimitSelect() {
    public SelectOrderByStep from(SelectSelectStep select) {
        return select.from(T("user_table"))
            .where(jt.conditions("name%", name, "birthday>=", beginDate));
    }
    public List<OrderField> orderBy() {
        return Arrays.asList(F("birthday").desc());
    }
};

// Mode 1: return all data, no total count
LimitResult res1 = jt.query(User.class, limitSelect);

// Mode 2: return limit rows, no total count
LimitResult res2 = jt.query(User.class, limitSelect, LimitRange.of(20));

// Mode 3: paginate (offset starts at 0), calculate total count
LimitResult res3 = jt.query(User.class, limitSelect,
    LimitRange.of(20, 0));
// res3.getResult() returns data, res3.getTotal() returns total count

// Mode 4: specify result fields
LimitResult res4 = jt.query(User.class, limitSelect,
    LimitRange.of(20, 0), Arrays.asList("id", "name"));

// LimitRange.all(): return all data, no total count
LimitResult res5 = jt.query(User.class, limitSelect, LimitRange.all());

// Access results
List<User> data = res3.getResult();
int total = res3.getTotal();

Enter fullscreen mode Exit fullscreen mode

About the LimitSelect interface:

// LimitSelect is a interface:
public interface LimitSelect {
    // Build the FROM clause; the select parameter allows specifying query fields
    SelectOrderByStep from(SelectSelectStep select);

    // Optional: return order fields; return null to skip ordering
    default List<OrderField> orderBy() { return null; }
}

Enter fullscreen mode Exit fullscreen mode

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다