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. 【RMAN】使用RMAN增量备份刷新 Standby Database

    Step 1: Create the Incremental Backup RMAN> BACKUP DEVICE TYPE DISK INCREMENTAL FROM SCN 750983 D ...

  2. 搭建Maven环境——使用本地的maven环境

    1.安装JDK. 2.Maven是 Apache 下的一个项目,官网下载 Maven:http://maven.apache.org/download.cgi 系统变量:M2_HOME= G:\vis ...

  3. [从jQuery看JavaScript]-JavaScript

    什么是JavaScript?相信随便百度Google一下都能找到一大堆的定义解释.而在我的理解中,JavaScript就是一种客户端的脚本语言,用于处理页面数据逻辑和用户体验(网页特效).实际上,Ja ...

  4. Web下的整体测试 --性能测试及优化思路

    随着Internet的日益普及,现在基于B/S结构的大型应用越来越多,可如何对这些应用进行测试成为日益迫切的问题.有许多测试人员来信问我B/S的测试如何做,由于工作较繁忙,对大家提出的问题也是头痛医头 ...

  5. javascript文本格式化之HTML标签(转载)

    文本格式化标签: 标签 描述 <b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> ...

  6. try catch 异常处理

    1.捕获指定异常 2.捕获所有异常(catch(...))

  7. (转)loff_t *ppos是什么东东

    ssize_t generic_file_read(struct file * filp, char * buf, size_t count, loff_t *ppos) 这是一个文件读函数 我们很容 ...

  8. JMeter使用记录2 -- Web測试

    Web測试用例 新建线程组.配置起3线程,循环1次.1秒启动全部线程.进行login.循环1个主要页面的读写,并对另外一个主要页面进行读操作,最后logout并在aggregate graph/gra ...

  9. par函数的xaxt函数-控制x轴刻度的显示

    xaxt 参数控制x轴的刻度以及刻度对应的标签时候显示 默认值为‘s’, 表示显示,代码示例 par(xaxt = 's') plot(1:5, 1:5, main = "title&quo ...

  10. MathType公式波浪线怎么编辑

    数学公式中有很多符号与数学样式,在用手写时是没有问题的,但是很多论文或者期刊中也是需要用到这些符号或者样式的,比如公式波浪线,那么MathType公式波浪线怎么编辑出来呢? 具体操作步骤如下: 1.打 ...