Vijay Leo
Vijay Leo

Reputation: 109

How to use pagination in spring mvc and hibernate application

I have developed spring MVC + MySQL and hibernate application

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..

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.

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.

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.

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.

Upvotes: 2

Views: 11774

Answers (3)

kushal jain
kushal jain

Reputation: 147

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.

in controller send a variable pagination that shows how much pagination

in Hibernate use

example:
        query.setFirstResult(1);
        query.setMaxResults(10);

on jsp follow this. code is bigger.

                         <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>

Upvotes: 0

geoand
geoand

Reputation: 63991

Since you are using Spring Data JPA, check out the extremely helpful PagingAndSortingRepository interface. This tutorial will show you all you need to see how it used

Upvotes: 3

Kanwaljeet Singh
Kanwaljeet Singh

Reputation: 1225

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.

Upvotes: 0

Related Questions