简体   繁体   English

没有Spring数据的分页

[英]Pagination Without Spring Data

I am creating a web application using Spring MVC and my database isn't supported by Spring Data. 我正在使用Spring MVC创建一个Web应用程序,但是Spring Data不支持我的数据库。 How can I add pagination without using Spring Data. 如何在不使用Spring Data的情况下添加分页。 My application has a table which list out objects retrieved via a service which has the ability to retrieve paginated results(i am able to pass in the page and result size). 我的应用程序有一个表,该表列出了通过服务检索的对象,该服务具有检索分页结果的能力(我能够传递页面和结果大小)。 The problem comes when the user searches by date range or any other search where parameters are placed in the url. 当用户按日期范围或在URL中放置参数的任何其他搜索进行搜索时,就会出现问题。 I cannot figure out a way to generate the correct url for the "next" and "prev" links. 我无法找出一种方法来为“下一个”和“上一个”链接生成正确的网址。

If your database isn't supported by Spring Data you can reuse class PageImpl to wrap your result and not use page data in queries, just wrap your search result. 如果Spring Data不支持您的数据库,则可以重用PageImpl类包装结果,而不在查询中使用页面数据,只需包装搜索结果即可。

List<T> content = your result set;
Pageable pageable= new PageRequest(int page, int size);
long total = cound found elements;
PageImpl pager = new PageImpl(content,pageable,cound );

in this case, you just reuse class and you don't need to work with calculation. 在这种情况下,您只需重用类即可,而无需进行计算。 class PageRequest has previous(), next() methods, so you don't need to calculate it. PageRequest类具有previous(), next()方法,因此您无需计算它。

If you don't want to have spring data as dependency you can just copy and put into your project page classes that you need. 如果您不想将spring数据作为依赖项,则可以复制并放入所需的项目页面类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM