Java 如何在spring mvc和hibernate应用程序中使用分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22830430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use pagination in spring mvc and hibernate application
提问by Vijay Leo
I have developed spring MVC + MySQL and hibernate application
我已经开发了 spring MVC + MySQL 和 hibernate 应用程序
It handles the more than thousands of records. Here, I am using dataTable for displaying records. It working fine. But, my problem is performance issue for record loading..
它处理超过数千条记录。在这里,我使用 dataTable 来显示记录。它工作正常。但是,我的问题是记录加载的性能问题..
When i execute the query in db server, it takes fraction of seconds to loading a thousands of records. but while i using a query via my app it takes more than 5 sec.
当我在 db 服务器中执行查询时,加载数千条记录需要几秒钟的时间。但是当我通过我的应用程序使用查询时,它需要超过 5 秒。
I think the dataTable pagination takes too much time to loading the records.. How can i resolve this type of issues. I have realize the solution is only for server side pagination.
我认为 dataTable 分页需要太多时间来加载记录.. 我该如何解决此类问题。我已经意识到该解决方案仅适用于服务器端分页。
I don't know how to make a these type of pagination like "Prev 1 2 3 4 5 Next" with searching and sorting controls in server side.
我不知道如何在服务器端使用搜索和排序控件制作这些类型的分页,例如“Prev 1 2 3 4 5 Next”。
So, please advice me how can i display the records from above requirements in my app. small piece code may more helpful for my knowledge growth.
所以,请建议我如何在我的应用程序中显示上述要求的记录。小块代码可能对我的知识增长更有帮助。
回答by kushal jain
I have recently implemented it. Follow the steps.
我最近实施了它。按照步骤。
whenever a url is called you send total number of data-List element and than use jstl to sow buttons as per your need total%10 or using any algorithm.
每当调用 url 时,您都会发送数据列表元素的总数,然后根据您的需要使用 jstl 播种按钮总数%10 或使用任何算法。
in controller send a variable pagination that shows how much pagination
在控制器中发送一个变量分页,显示多少分页
in Hibernate use
在 Hibernate 中使用
example:
query.setFirstResult(1);
query.setMaxResults(10);
on jsp follow this. code is bigger.
在jsp上遵循这个。代码更大。
<c:if test="${not empty pendingSTT}">
<div class="pagination">
<script type="text/javascript">
function setBP(val){
$("#BP").val(val);
}
</script>
<form:form commandName="register" action="/veepropbeta/pendingshare" method="GET">
<form:hidden path="BP" id="BP" />
<ul>
<c:set var="hello" value="0" />
<c:if test="${not empty param['BP']}">
<c:set var="hello" value="${param['BP']}" />
</c:if>
<c:if test="${hello>0}">
<li><a href=""><button class="btn btn-default" onclick="return setBP(this.value-1);" value="${hello}">Prev</button></a></li>
</c:if>
<c:if test="${not empty param['BP']}">
<c:set var="hello" value="${param['BP']}" />
</c:if>
<c:forEach begin="${hello}" end="${pagination-1}" var="limit" varStatus="status" >
<c:if test="${status.count<=10}" >
<li><a href=""> <button class="btn btn-default" style="float: left;" onclick="return setBP(this.value-1);" value="${limit+1}">${limit+1}</button></a></li>
<c:set var="last" value="${status.count}" />
</c:if>
<c:if test="${status.count>10}" >
<c:set var="ending" value="true" />
</c:if>
</c:forEach>
<c:if test="${ending.equals('true')}">
<li><a href=""><button class="btn btn-default" onclick="return setBP(this.value-1);" value="${last+1}">Next</button></a></li>
</c:if>
</ul>
</form:form>
</div>
</c:if>
回答by Kanwaljeet Singh
I was facing the same problem, I am using Smart GWT in my project ,which doesn't have pagination feature.
So, only option left for me was to use pagination on the server side.
I changed my service, and made it take 4 parameters start
,end
,sort-field
,sort-direction
.
The service would return from the database start
to end
number of records which will be grouped by sort-field
(one of the column of your dataTable) and sort-direction
is either ASC
or DESC
.
This way user can choose the number of records he/she wants to see per page(which can be predefine values in a drop down), and you can retrieve from service those number of records(by change start
and end
, and calling the service). You wouldn't have to face the problem of getting thousands of records in one go.
我遇到了同样的问题,我在我的项目中使用了 Smart GWT,它没有分页功能。
所以,留给我的唯一选择是在服务器端使用分页。我更改了我的服务,并使其采用 4 个参数start
, end
, sort-field
, sort-direction
。
该服务将从数据库返回start
到end
将按sort-field
(数据表的列之一)分组的记录数,并且sort-direction
是ASC
或DESC
。
通过这种方式,用户可以选择他/她希望每页查看的记录数(可以是下拉列表中的预定义值),并且您可以从服务中检索这些记录数(通过更改start
和end
,并调用服务)。您不必面对一次性获取数千条记录的问题。