Skip to content

core-java-samples/pagination-dto-mapping-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pagination with DTO Mapping — Sample

A minimal demonstration of pagination with layer-to-layer DTO mapping in Java.

What It Demonstrates

Extends the basic pagination sample with DTO mapping. The repository returns domain objects — Customer. The application layer converts the result into view objects — CustomerView — without rebuilding pagination metadata.

The key idea: PageResult.map() transforms the content across layers while preserving page number, size and total count unchanged.

Structure

The entire project intentionally fits in a single file — so the mechanics are visible in full, without unnecessary noise:

PaginationDtoMapping.java
│
├── Customer                # Domain model — record with id and name
├── CustomerView            # DTO — read model for the application layer
├── CustomerMapper          # Maps Customer → CustomerView
├── PageRequest             # Pagination request — page number and size
├── PageResult<T>           # Pagination result — content + metadata + map()
├── CustomerRepository      # Data access — returns PageResult<Customer>
├── FakeCustomerTable       # In-memory data source — simulates a database table
└── PaginationDtoMapping    # Entry point + demo()

Key Points

PageResult.map() converts the content type without touching pagination metadata — page number, size and total are preserved automatically.

Each layer owns its transformation. The repository returns PageResult<Customer>. The application layer maps it to PageResult<CustomerView> in a single call.

CustomerMapper holds the mapping logic — domain objects and view objects stay clean with no cross-layer constructors.

Console Output

PageResult[content=[CustomerView[id=1, name=Alice], CustomerView[id=2, name=Bob]], page=0, size=2, total=5]

The repository returned Customer records. The application layer received CustomerView records. Pagination metadata passed through unchanged.

About

Offset-based pagination with DTO mapping in pure Java.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages