Home > Java > javaTutorial > body text

Sharing a simple example of implementing paging function in Java

黄舟
Release: 2017-08-08 10:22:30
Original
1011 people have browsed it

This article mainly introduces the simple paging function in Java in detail, which has certain reference value. Interested friends can refer to it

The example in this article shares with you the paging function in Java. The specific code is for your reference. The specific content is as follows

No need to change the SQL form to query;
Query all the data directly and automatically display the data according to the page number;

Paging object


public class PageUtils implements Serializable {

  /**
   * 
   */
  private static final long serialVersionUID = -5247614532234782640L;
  public final static String PAGE = "page";
  public final static String PAGE_NO = "pageno";
  public final static String PAGE_SIZE = "pagesize";

  private long pageSize=10;//每页显示记录数
  private long firstResult=0;//当页第一条记录号
  private long totalCount;//总记录数
  private long totalPage;//总页码
  private long pageNo=1;//当前页码
  private List<?> sumData;//此集合可用来保存 合计数据
  private List<?> data;//查询结果

  public long getPageSize() {
    return pageSize;
  }
  public void setPageSize(long pageSize) {
    this.pageSize = pageSize;
  }
  public long getFirstResult() {
    if(pageNo>0){
      firstResult=pageSize * (pageNo -1);
    }else{
      firstResult = 0;
    }
    return firstResult;
  }

  public long getNextPageResult(){
    if(pageNo>0){
      return pageSize*(pageNo-1);
    }else{
      return pageNo;
    }
  }

  public void setFirstResult(long firstResult) {
    this.firstResult = firstResult;
  }
  public long getTotalCount() {
    return totalCount;
  }
  public void setTotalCount(long totalCount) {
    this.totalCount = totalCount;
    totalPage = this.totalCount/pageSize;
    if (totalPage == 0 || totalCount % pageSize != 0) {
      totalPage++;
    }
  }
  public long getTotalPage() {
    return totalPage;
  }
  public void setTotalPage(long totalPage) {
    this.totalPage = totalPage;
  }
  public long getPageNo() {
    return pageNo;
  }
  public void setPageNo(long pageNo) {
    this.pageNo = pageNo;
  }

  public List<?> getData() {
    return data;
  }

  public void setData(List<?> data) {
    this.data = data;
  }

  /**
   * 是否第一页
   */
  public boolean isFirstPage() {
    return pageNo <= 1;
  }

  /**
   * 是否最后一页
   */
  public boolean isLastPage() {
    return pageNo >= getTotalPage();
  }

  /**
   * 下一页页码
   */
  public long getNextPage() {
    if (isLastPage()) {
      return pageNo;
    } else {
      return pageNo + 1;
    }
  }

  /**
   * 上一页页码
   */
  public long getPrePage() {
    if (isFirstPage()) {
      return pageNo;
    } else {
      return pageNo - 1;
    }
  }

  public PageUtils(){}

  public PageUtils(long pageNo){
    this.pageNo=pageNo;
  }

  public PageUtils(long pageNo,long pageSize){
    this.pageNo=pageNo;
    this.pageSize = pageSize;
  }

  public List<?> getSumData() {
    return sumData;
  }
  public void setSumData(List<?> sumData) {
    this.sumData = sumData;
  }

}
Copy after login

Query data entity

Add the page number and each page to the query entity Display number parameters;


private int pageSize;  //每页显示的条数
private int pageNo;   //当前页码
public int getPageSize() {
  return pageSize;
}
public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
}
Copy after login

Control layerController


##

@RequestMapping("/list")
public String list(Model model,ChannelValueInfoView input) {
    // input:传入的参数为对象

    PageUtils page=new PageUtils();
    //如果传入的当前条数为0,则赋予值(首次查询不带参);
    if(input.getPageSize()==0){
      //当前页码第一页
      input.setPageNo(1);
      //每页显示条数,当前每页显示10条数据;
      input.setPageSize(10);
    }
    page.setPageNo(input.getPageNo());
    page.setPageSize(input.getPageSize());
    //核心分页代码
    PageHelper p=new PageHelper();   
    Page<ChannelValueInfoList> l=p.startPage(input.getPageNo(),input.getPageSize());
    //紧跟着的第一个select查询将会被分页
    channelValueService.getChannelValueInfoViewList(input);
    model.addAttribute("input", input);
    page.setData(l);
    page.setTotalCount(l.getTotal());
    model.addAttribute("page", page);
    return "index";
  }
Copy after login

Page processing


//循环穿过来的PAGE.data数据
<tr th:each="ts : ${page.data}">
<td th:text="${ts.channelValueName}"></td>


----------
<form id="content_form" action="/channelValue/list" method="post" >
  <p>
    总数:<span id="totalCount" th:text="${page.totalCount}">0</span>
  </p>
  <ul class="pagination">
    <li class="disabled">
      <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onFirst()">首页</a>
    </li>
    <li class="disabled">
      <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onPre()"><</a>
    </li>
    <li class="active">
      <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <span id="beginRow" th:text="${page.pageNo}">0</span>
      </a>
    </li>
    <li class="disabled">
      <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onNext()">></a>
    </li>
    <li class="disabled">
      <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onLast()">尾页</a>
    </li>
  </ul>
</for  m>


----------

<script>
  function onFirst() {
    onList(1);
  }
  function onPre() {
    var beginRow = parseInt($(&#39;#beginRow&#39;).html());
    if (beginRow - 1 > 0) {
      onList(beginRow - 1);
    }

  }
  function onNext() {
    var beginRow = parseInt($(&#39;#beginRow&#39;).html());
    var totalCount = parseInt($(&#39;#totalCount&#39;).html());
    var pageSize = parseInt($(&#39;#pageSize&#39;).val());
    if (parseInt(totalCount / pageSize + 1) > beginRow + 1) {
      onList(beginRow+1);
    }
  }
  function onLast() {
    var totalCount = parseInt($(&#39;#totalCount&#39;).html());
    var pageSize = parseInt($(&#39;#pageSize&#39;).val());
    onList(parseInt(totalCount / pageSize + 1) - 1);
  }
  function onList(pageNo) {
    if (pageNo == 0)
      pageNo = 1;
    $(&#39;#pageNo&#39;).val(pageNo);
    $("#content_form").submit();
  }
</script>
Copy after login

The above is the detailed content of Sharing a simple example of implementing paging function in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template