欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringData JPA实现查询分页demo

程序员文章站 2024-03-05 10:39:54
springdata jpa 的 pagingandsortingrepository接口已经提供了对分页的支持,查询的时候我们只需要传入一个 org.springfram...

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。