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. ADT Android Development Tools

    ADT(Android Development Tools)在Eclipse编译IDE环境中,需安装ADT(Android Developer Tools)Plug-in,这是Android在Ecli ...

  2. 【转】C# 调用WebService的方法

    很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...

  3. sendEvent()

    sendEvent()(QObject *receiver, QEvent *event)可以将自己创建事件event发送给制定的receiver 例如, //创建按键事件 QKeyEvent key ...

  4. Windows下 flex + bison 小例子

    .下载flex和bison,网址是http://gnuwin32.sourceforge.net/packages/flex.htm 和http://gnuwin32.sourceforge.net/ ...

  5. C++ 有用的书籍

    C++ 有用的书籍Essential C++ 中文版C++ Primer Plus 第6版中文版C++ Primer中文版(第5版) #include <iostream> /* run ...

  6. mysql -- 循环插入数据到表中

    备忘: 1.经搜索发现,MySql不支持直接写SQL语句实现循环插入功能. 想要实现该功能,可以用其他语言操控MySql来实现,或者用存储过程来实现(Store Procedure--SP).   2 ...

  7. postman从入门到精通

    今天总监让我给测试同事们培训postman,使用过postman的朋友应该知道,这个简直就是前后端接口调试神器.根据平时的经验以及自己到网上看了相关的帖子,对于postman又有了新的认识. post ...

  8. ImportError: No module named Crypto.Cipher

    from Crypto.Cipher import AES 报错: ImportError: No module named Crypto.Cipher 解决方法: pip install pycry ...

  9. is_file,is_dir,file_exists

    is_file()和file_exists()效率比较,结果当文件存在时,is_file函数比file_exists函数速度快14倍,当文件不存在时,两者速度相当.同理,当文件目录存在时,is_dir ...

  10. db2

    关于3种导入导出操作进行简单的介绍:export:导出数据,支持IXF,DEL或WSFimport:导入数据,可以向表中导入数据,支持上面提到的4种文件类型.    load:导入数据,功能和impo ...