Paginated queries with automatic total count calculation. Supports specifying result fields.
public <E> LimitResult<E> query(Class<E> cls, LimitSelect limitSelect, LimitRange range)
public <E> LimitResult<E> query(Class<E> cls, LimitSelect limitSelect, SelectList selectList, LimitRange range)
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 limit rows, no total count
LimitResult res1 = jt.query(User.class, limitSelect, LimitRange.of(20));
// Mode 2: paginate (offset starts at 0), calculate total count
LimitResult res2 = jt.query(User.class, limitSelect,
LimitRange.of(20, 0));
// res2.getResult() returns data, res3.getTotal() returns total count
// Mode 3: specify result select list
LimitResult res3 = jt.query(User.class, limitSelect,
SL("id", "name"),LimitRange.of(20, 0));
// LimitRange.all(): return all data, no total count
LimitResult res4 = jt.query(User.class, limitSelect, LimitRange.all());
// Access results
List<User> data = res2.getResult();
int total = res2.getTotal();
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; }
}
Top comments (0)