I have 3000 records in an employee table which I have fetched from my database with a single query. I can show 20 records per page. So there will be 150 pages for with each page
I assume you have a bean class representing records in this table, with instances loaded from whatever ORM you have in place.
If you haven't already, you should implement caching of these beans in your application. This can be done locally, perhaps using Guava's CacheBuilder, or remotely using calls to Memcached for example (the latter would be necessary for multiple app servers/load balancing). The cache for these beans should be keyed on a unique id, most likely the mapping to the primary key column of the corresponding table.
Getting to the pagination: simply write your queries to return only IDs of the selected records. Include LIMIT
and OFFSET
or your DB language's equivalent to paginate. The caller of the query can also filter/sort at will using WHERE
, ORDER BY
etc.
Back in the Java layer, iterate through the resulting IDs of these queries and build your List
of beans by calling the cache. If the cache misses, it will call your ORM to individually query and load that bean. Once the List
is built, it can be processed/serialized and sent to the UI.