如何在hibernate jpa中创建一个分页组件,

时间:2016-01-04 14:00:57

标签: hibernate spring-data-jpa

这是我从数据库中获取城市表的控制器..有没有办法创建一个分页组件,在我的其他JSP中一次又一次地使用它。所以我只是调用组件并传递参数...

@RequestMapping(value = "/city")
    public ModelAndView getCityPaginatedPage(@RequestParam(value="pageNumber",required = false) Integer pageNumber,@ModelAttribute ("model") AdminCityModel CityModel, BindingResult result, ModelAndView model) {

        if(pageNumber==null){
            pageNumber=1;
        }

        Page<AdminCityModel> page = service.getCityListFrom1To10(pageNumber);
        List<AdminCityModel> CityList =page.getContent();

        int current = page.getNumber() + 1;
        int begin = Math.max(1, current - 5);
        int end = Math.min(begin + 10, page.getTotalPages());
        model.addObject("page", page);
        model.addObject("CityList", CityList);
        model.addObject("beginIndex", begin);
        model.addObject("endIndex", end);
        model.addObject("currentIndex", current);
        model.setViewName("admin/city");
        return model;
    }

这是我的服务..

public interface AdminCityService {

public Page<AdminCityModel> getCityListFrom1To10(Integer pageNumber);

}

这是我的服务实施

@Transactional
public Page<AdminCityModel> getCityListFrom1To10(Integer pageNumber) {
    PageRequest request =
            new PageRequest(pageNumber - 1,Constants.PAGE_SIZE, Sort.Direction.ASC, "id");
    Page<AdminCityModel> res= repository.findAll(request);
    return res;
}

这是我的Jsp

<form id="updatecity">
                    <table id="cityTable" class="table table-responsive table-hover" style="width: 50%">
                        <thead>
                              <tr>
                                <th style = "text-align: center;">Id</th>
                                <th style = "text-align: center;">City Name</th>
                                <th style = "text-align: center;">Change City</th>
                              </tr>
                        </thead>
                        <tbody>
                            <c:forEach items="${CityList}" var = "ctable" >
                                <tr>
                                    <td id="id" style = "text-align: center;">
                                        <c:out value="${ctable.id}"></c:out>
                                    </td>
                                    <td id="city" style = "text-align: center;">
                                        <c:out value="${ctable.city}"></c:out>
                                    </td>
                                    <td style = "text-align: center;">
                                        <button type="button"  class="btn btn-default editbtn">Edit</button>
                                    </td>
                                 </tr>
                            </c:forEach>
                        </tbody>
                    </table>
    <c:url var="firstUrl" value="/admin/city?pageNumber=1" />
                    <c:url var="lastUrl" value="/admin/city?pageNumber=${page.totalPages}" />
                    <c:url var="prevUrl" value="/admin/city?pageNumber=${currentIndex - 1}" />
                    <c:url var="nextUrl" value="/admin/city?pageNumber=${currentIndex + 1}" />

<div class="pagination" id="pagination">
                        <ul>
                            <c:choose>
                                <c:when test="${currentIndex == 1}">
                                    <li class="disabled"><a href="#">&lt;&lt;</a></li>
                                    <li class="disabled"><a href="#">&lt;</a></li>
                                </c:when>
                                <c:otherwise>
                                    <li><a href="${firstUrl}">&lt;&lt;</a></li>
                                    <li><a href="${prevUrl}">&lt;</a></li>
                                </c:otherwise>
                            </c:choose>
                            <c:forEach var="i" begin="${beginIndex}" end="${endIndex}">
                                <c:url var="pageUrl" value="/admin/city?pageNumber=${i}" />
                                <c:choose>
                                    <c:when test="${i == currentIndex}">
                                        <li class="active"><a href="${pageUrl}"><c:out value="${i}" /></a></li>
                                    </c:when>
                                    <c:otherwise>
                                        <li><a href="${pageUrl}"><c:out value="${i}" /></a></li>
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                            <c:choose>
                                <c:when test="${currentIndex == page.totalPages}">
                                    <li class="disabled"><a href="#">&gt;</a></li>
                                    <li class="disabled"><a href="#">&gt;&gt;</a></li>
                                </c:when>
                                <c:otherwise>
                                    <li><a href="${nextUrl}">&gt;</a></li>
                                    <li><a href="${lastUrl}">&gt;&gt;</a> </li>
                                </c:otherwise>
                            </c:choose>
                        </ul>
                    </div>
                </form>

2 个答案:

答案 0 :(得分:0)

您可以创建将成为每个请求一部分的分页对象。创建分页类为

public class PaginationPrameters{

     private int pageIndex;
     private int rowsPerPage;
     private int startingIndex;

}

请求将包含所有三个字段,并在控制器中构造对象。一旦你有了这个对象,就可以计算下一个请求分页边界并用它来更新查询。

答案 1 :(得分:0)

你问题上的标签说你正在使用弹簧数据?

然后很简单,spring-data有build-in interface支持分页。让我们重写您的AdminCityService:

PageRequest request = new PageRequest(0, 10);
Page<AdminCityModel> adminCityModels = AdminCityService.getCityList(request);

如果您需要页面大小为10并且想要检索前10个AdminCityModel,您可以创建PageRequest(它实现Pageable,page-index从零开始)并将其传递给您的方法:

{{1}}

Page为您提供了一些有用的getter,可以在您的视图中创建一个漂亮的视图。