SpringData JPA實(shí)現(xiàn)查詢分頁(yè)demo
SpringData JPA 的 PagingAndSortingRepository接口已經(jīng)提供了對(duì)分頁(yè)的支持,查詢的時(shí)候我們只需要傳入一個(gè) org.springframework.data.domain.Pageable
接口的實(shí)現(xiàn)類,指定PageNumber和pageSize即可
springData包中的 PageRequest類已經(jīng)實(shí)現(xiàn)了Pageable接口,我們可以直接使用下邊是部分代碼:
DAO:
package com.jiaoyiping.jdjy.sourcecode.dao; import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode; import org.springframework.data.repository.PagingAndSortingRepository; /** * Created with IntelliJ IDEA. * User: 焦一平 * Date: 14-11-20 * Time: 下午11:18 * To change this template use File | Settings | File Templates. */ public interface SourceCodeDao extends PagingAndSortingRepository<SourceCode, String> { }
service:
package com.jiaoyiping.jdjy.sourcecode.service; import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode; import com.jiaoyiping.jdjy.sourcecode.dao.SourceCodeDao; import org.apache.solr.client.solrj.SolrServerException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import javax.transaction.Transactional; import java.io.IOException; import java.sql.Timestamp; import java.util.List; /** * Created with IntelliJ IDEA. * User: 焦一平 * Date: 14-11-20 * Time: 下午11:24 * To change this template use File | Settings | File Templates. */ public class SourceCodeService { @Autowired private SourceCodeDao sourceCodeDao;public Page<SourceCode> getSourceCode(int pageNumber,int pageSize){ PageRequest request = this.buildPageRequest(pageNumber,pageSize); Page<SourceCode> sourceCodes= this.sourceCodeDao.findAll(request); return sourceCodes; } //構(gòu)建PageRequest private PageRequest buildPageRequest(int pageNumber, int pagzSize) { return new PageRequest(pageNumber - 1, pagzSize, null); } }
controller:
package com.jiaoyiping.jdjy.sourcecode.controller; import com.jiaoyiping.jdjy.sourcecode.Const; import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode; import com.jiaoyiping.jdjy.sourcecode.service.SourceCodeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created with IntelliJ IDEA. * User: 焦一平 * Date: 14-11-20 * Time: 下午11:22 * To change this template use File | Settings | File Templates. */ @Controller @RequestMapping(value = "/sourcecode") public class SourceCodeController { @Autowired private SourceCodeService sourceCodeService; @RequestMapping(value = "list") public ModelAndView listSourceCode(HttpServletRequest request, HttpServletResponse response){ String pageNumberStr=request.getParameter("pageNumber"); if(pageNumberStr==null ||"".equals(pageNumberStr)){ pageNumberStr="1"; } int pageNumber = Integer.parseInt(pageNumberStr); int pageSize = Const.PAGE_SIZE; ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/sourcecode/listSourceCode"); Page<SourceCode> sourceCodes = this.sourceCodeService.getSourceCode(pageNumber, pageSize); modelAndView.addObject("sourceCodeList",sourceCodes.getContent()); modelAndView.addObject("totalPageNumber",sourceCodes.getTotalElements()); modelAndView.addObject("pageSize",pageSize); return modelAndView; } }
前端分頁(yè):
前端分頁(yè)組件我們使用bootstrap提供的分頁(yè)組件:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%-- Created by IntelliJ IDEA. User: 焦一平 Date: 2014/12/27 Time: 9:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String basePath = request.getContextPath(); String MethodURL=basePath+"/sourcecode/list.action?pageNumber="; %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>源代碼列表</title> <link href="<%=basePath%>/resources/assets/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"/> <script type="text/javascript" src="<%=basePath%>/resources/js/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ var totalNumber = Number(${totalPageNumber}); var pageSize = Number(${pageSize}); var pageCount = totalNumber/pageSize; var html = ""; for(var i = 0;i<pageCount;i++){ var link_Url = "<li><a href=\"<%=MethodURL%>"+(i+1)+"\">"+(i+1)+"</a></li>"; html += link_Url; } var fenyeDiv = document.getElementById("link"); fenyeDiv.innerHTML=html; }); </script> </head> <body> <a href="#" rel="external nofollow" class="list-group-item active"> 源代碼列表 </a> <c:forEach items="${sourceCodeList}" var="sourceCode"> <a href="<%=request.getContextPath()%>/sourcecode/detail.action?id=<c:out value=" rel="external nofollow" ${sourceCode.id}" />" class="list-group-item"><c:out value="${sourceCode.title}" /></a> </c:forEach> <!-- 列表分頁(yè)的DIV,由JS動(dòng)態(tài)填充內(nèi)容--> <ul class="pagination pagination-lg" id="link"> </ul><br> </body> </html>
最終結(jié)果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringData JPA快速上手之關(guān)聯(lián)查詢及JPQL語(yǔ)句書寫詳解
- SpringData JPA中查詢接口Repository的使用
- SpringDataJpa如何使用union多表分頁(yè)條件查詢
- SpringDataJpa like查詢無(wú)效的解決
- SpringDataJPA之Specification復(fù)雜查詢實(shí)戰(zhàn)
- springdata jpa使用Example快速實(shí)現(xiàn)動(dòng)態(tài)查詢功能
- SpringDataJPA原生sql查詢方式的封裝操作
- SpringData JPA Mongodb查詢部分字段問(wèn)題
相關(guān)文章
SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法
本篇文章主要介紹了SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05JavaWeb servlet實(shí)現(xiàn)下載與上傳功能的方法詳解
這篇文章主要介紹了JavaWeb servlet實(shí)現(xiàn)下載與上傳功能的方法,結(jié)合實(shí)例形式詳細(xì)分析了JavaWeb servlet實(shí)現(xiàn)下載與上傳功能的原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-04-04java實(shí)現(xiàn)簡(jiǎn)單的加減乘除計(jì)算器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的加減乘除計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Springboot實(shí)現(xiàn)緩存預(yù)熱的方法
在系統(tǒng)啟動(dòng)之前通過(guò)預(yù)先將常用數(shù)據(jù)加載到緩存中,以提高緩存命中率和系統(tǒng)性能的過(guò)程,緩存預(yù)熱的目的是盡可能地避免緩存擊穿和緩存雪崩,這篇文章主要介紹了Springboot實(shí)現(xiàn)緩存預(yù)熱,需要的朋友可以參考下2024-03-03JavaCV使用ffmpeg實(shí)現(xiàn)錄屏功能
這篇文章主要介紹了JavaCV如何使用ffmpeg實(shí)現(xiàn)錄屏功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06詳解Java的TCP/IP編程學(xué)習(xí)--基于定界符的成幀
這篇文章主要介紹了Java的TCP/IP編程學(xué)習(xí)--基于定界符的成幀,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04SpringBoot+MinIO實(shí)現(xiàn)文件切片極速詳解
在現(xiàn)代Web應(yīng)用中,文件上傳是一個(gè)常見的需求,尤其是對(duì)于大文件的上傳,如視頻、音頻或大型文檔,所以本文就來(lái)為大家介紹一下如何使用Spring Boot和MinIO實(shí)現(xiàn)文件切片極速上傳技術(shù)吧2023-12-12深入解析Spring Cloud內(nèi)置的Zuul過(guò)濾器
這篇文章主要給大家深入的介紹了Spring Cloud內(nèi)置的Zuul過(guò)濾器的相關(guān)資料,文中給大家介紹的很詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-02-02