Skip to content

java-backend-architecture/persistence-flat-pagination-jdbc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

persistence-flat-pagination-jdbc

A minimal JDBC example of offset-based pagination for flat entity queries — no Spring Data, pure SQL, clean architecture.

What it shows

  • Offset-based pagination with LIMIT / OFFSET and COUNT(*)
  • Using package-private classes as an encapsulation boundary inside the infrastructure layer
  • Separating persistence projections from application-level read models via ViewMapper

Stack

  • Java 25
  • Spring Boot
  • Spring JDBC (JdbcClient)
  • H2 (in-memory database)

Structure

application/
    OwnerReadRepository   ← port (interface)
    OwnerView             ← read model
    PageQuery             ← pagination input
    PageResult<T>         ← pagination output

infrastructure/
    JdbcOwnerReadRepository   ← JDBC implementation
    OwnerProjection           ← internal, never leaks out
    ViewMapper                ← projection → view

How it works

// page 0, 2 items per page
repository.findAllFlat(new PageQuery(0, 2));

// page 1, 2 items per page
repository.findAllFlat(new PageQuery(1, 2));

Output:

=== page - 0 ===
PageResult[content=[OwnerView[id=1, name=jack1], OwnerView[id=2, name=jack2]], page=0, size=2, total=10]

=== page - 1 ===
PageResult[content=[OwnerView[id=3, name=jack3], OwnerView[id=4, name=jack4]], page=1, size=2, total=10]

PageQuery and PageResult have zero framework dependencies — copy them into any Java project.

Tests

Integration tests in src/test/java cover pagination correctness, last page with remainder, and edge cases including pages beyond total.

Related

Run

./mvnw spring-boot:run

About

Offset-based pagination for flat entity queries using JDBC — no Spring Data, pure SQL, clean architecture.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages