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. Java实现的各种排序算法(包括冒泡,快排等)

    //堆排序 不稳定 import java.util.Arrays; public class HeapSort { public static void main(String[] args) { ...

  2. linux设置开机服务自动启动/关闭自动启动命令

    linux设置开机服务自动启动/关闭自动启动命令 2012-02-06 15:13 [root@localhost ~]# chkconfig --list     显示开机可以自动启动的服务[roo ...

  3. HTML文档的经常使用标记

    一.HTML文档中经常使用的标记有文字标记.段落标记.列表标记.超链接标记.图像标记.表格标记.框架标记和多媒体标记,以下对这些经常使用标记进行介绍: 1.文字标记:文字是网页重要的组成部分之中的一个 ...

  4. java- Servlet-session

    Servlet 相关:http://blog.csdn.net/ggGavin/article/category/2134213 Servlet——Session(1)之基础知识 Servlet——S ...

  5. 使用powerdesinger逆向生成表结构

    (1).使powerdesigner建立和数据库的链接 (2)配置链接详情 (3) (4) (5)更新表结构 (6) (7) 附加:当有时候会报错时: 解决方式: (1.1)更改所连接的数据库 (1. ...

  6. C++11 新特性:Lambda 表达式

    参考文章:https://blogs.oracle.com/pcarlini/entry/c_1x_tidbits_lambda_expressions 或许,Lambda 表达式算得上是 C++ 1 ...

  7. Linux如何通过命令查看日志文件的某几行(中间几行或最后几行)

    linux 如何显示一个文件的某几行(中间几行) [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1 ...

  8. stos

    add <?php /* 添加脚本 参数:u=用户名 v=城市名 为用户添加城市标签 */ header("Content-Type:text/html; charset=utf-8& ...

  9. 关于SQL语句的一些注意事项

    1.Into 表后要编辑-IntelliSense-刷新本地缓存 才能访问新表 2.Is null不是=null

  10. chrome浏览器插件推荐——Vimium 篇

    Vimium 是chrome底下的一个插件,所有chrome浏览器或者是基于chrome内核的浏览器(比如我用的Vivaldi)都可以使用这个插件.它提供了大量快捷键来方便键盘党浏览网页.目前在Chr ...