jOOQ offset pagination
Spring Boot implementation of offset pagination can be easily shaped via LIMIT … OFFSET
(or OFFSET … FETCH
) and SELECT COUNT
. For instance, if we assume that a client gives us a page number (via the page
argument) and size
(the number of products to be displayed on page
), then the following jOOQ query mimics the default Spring Boot pagination behavior for the PRODUCT
table:
long total = ctx.fetchCount(PRODUCT); List<Product> result = ctx.selectFrom(PRODUCT) .orderBy(PRODUCT.PRODUCT_ID) .limit(size) .offset(size * page) .fetchInto(Product.class);
If the client (for instance, the browser) expects as a response a serialization of the classical Page<Product>
(org.springframework.data.domain.Page
), then you can simply produce it, as shown here:
Page<Product> pageOfProduct = new PageImpl(result, PageRequest.of(page, size, Sort.by(Sort.Direction.ASC, ...