SpringData JPA 的 PagingAndSortingRepository接口已经提供了对分页的支持,查询的时候我们只需要传入一个 org.springframework.data.domain.Pageable

接口的实现类,指定PageNumber和pageSize即可

springData包中的 PageRequest类已经实现了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;
}
  //构建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; } }

前端分页:

前端分页组件我们使用bootstrap提供的分页组件:

<%@ 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="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="#" class="list-group-item active">
源代码列表
</a>
<c:forEach items="${sourceCodeList}" var="sourceCode">
<a href="<%=request.getContextPath()%>/sourcecode/detail.action?id=<c:out value="${sourceCode.id}" />" class="list-group-item"><c:out value="${sourceCode.title}" /></a>
</c:forEach>
<!-- 列表分页的DIV,由JS动态填充内容-->
<ul class="pagination pagination-lg" id="link"> </ul><br> </body>
</html>

最终结果如下:

SpringData JPA查询分页demo的更多相关文章

  1. springdata jpa 关于分页@Query问题

    关于springdata jpa 分页问题相信很多小伙伴都遇到过,只要表中数量到达分页条件就会报错 废话少说直接上代码: @Query(nativeQuery = true, value = &quo ...

  2. 记一次JPA查询分页导致的数据丢失问题

    使用JPA查询,共17条数据,每页10条数据. 第一页与第二页有一条重复的数据,导致丢失一条数据 后查明原因发现,该查询使用了排序,排序字段的值在多条数据中相同,比如在3-11条是相同的值.此时跳到第 ...

  3. springdata jpa查询用like时候需要输入该属性的类型字节码

  4. 第11章—使用对象关系映射持久化数据—SpringBoot+SpringData+Jpa进行查询修改数据库

    SpringBoot+SpringData+Jpa进行查询修改数据库 JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分.但它又不限于EJB 3.0,你可以在Web应用.甚至桌面应用 ...

  5. SpringData JPA实现CRUD,分页与多参数排序

    Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库.Map-Reduce 框架.云数据服务等等,SpringData JPA是简化创建 JPA ...

  6. springdata jpa使用Example快速实现动态查询

    Example官方介绍 Query by Example (QBE) is a user-friendly querying technique with a simple interface. It ...

  7. SpringData JPA快速入门和基本的CRUD操作以及Specifications条件查询

    SpringData JPA概述: SpringData JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作 ...

  8. SpringData JPA使用JPQL的方式查询和使用SQL语句查询

    使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件, 这时就可以使用@Query注解,结合JPQL的语句方式完成查询 持久 ...

  9. spring data jpa Specification 复杂查询+分页查询

    当Repository接口继承了JpaSpecificationExecutor后,我们就可以使用如下接口进行分页查询: /** * Returns a {@link Page} of entitie ...

随机推荐

  1. 没有启动 ASP.NET State service错误的解决方法

    具体错误如下: 异常详细信息: System.Web.HttpException: 无法向会话状态服务器发出会话状态请求.请确保已启动 ASP.NET State service,并且客户端和服务器端 ...

  2. js元素绑定事件

    想给一个元素绑定一个方法之后,在绑定一个方法而且不被覆盖 window.onload = function () { alert('a'); } window.onlaod=function(){ a ...

  3. ASP.NET实现类似Excel的数据透视表

    代码: /Files/zhuqil/Pivot.zip 数据透视表提供的数据三维视图效果,在Microsoft Excel能创建数据透视表,但是,它并不会总是很方便使用Excel.您可能希望在Web应 ...

  4. HEVC/H.265 的未来必须是使用并行处理(OpenCL?) OpenCV和OpenCL区别

    1 扩展库简介 OpenCV(Open Source Computer Vision Library)是一个致力于实时处理计算机视觉问题的开源库.它最初由Intel公司开发,以GPL许可协议发布,后来 ...

  5. 为什么要整合apache 和tomcat?

    1. Apache是web服务器,Tomcat是应用(java)服务器,它只是一个servlet容器,是Apache的扩展. 2. Apache和Tomcat都可以做为独立的web服务器来运行,但是A ...

  6. dom元素改变监听

    function domChange(domId, callback) { // select the target node var target = document.getElementById ...

  7. JavaScript-事件冒泡简介及应用

    一.什么是事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个事 ...

  8. logging.xml file setfile(null,true) call failed

    定义目录三个方法:一:${catalina.base}或${catalina.home}相对路径配置方法.catalina.home是你配置服务器时自动在环境变量中加的路径,默认是指向tomcat服务 ...

  9. centos7 安装hadoop 集群遇到的问题

    集群安装之后,hdfs 不能上传文件,也提示rute等错误,其实是防火墙问题,关闭防火墙即可. CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙.firewa ...

  10. MathType编辑物理单位的方法

    在用MathType编辑物理公式时,由于物理单位很多都是复合单位,所以在编辑时如果能够有这种复合单位直接使用的话,编辑效率就会大大提高.实际上这种想法在MathType中是可行的,MathType中也 ...