首页 技术 正文
技术 2022年11月23日
0 收藏 772 点赞 2,818 浏览 4481 个字

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="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>
<!-- 列表分页的DIV,由JS动态填充内容-->
<ul class="pagination pagination-lg" id="link"></ul><br></body>
</html>

最终结果如下:

SpringData JPA查询分页demo

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/141855.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:875 阅读:5,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:806 阅读:3,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:565 阅读:4,317
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:730 阅读:4,311
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:4,911
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,099
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:8:00-16:00

客服电话

400-888-8888

客服邮箱

ceotheme@ceo.com

扫描二维码

关注微信公众号

扫描二维码

手机访问本站