服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现简单的分页功能

Java实现简单的分页功能

2020-12-12 15:44LandiYax Java教程

这篇文章主要为大家详细介绍了Java实现简单的分页功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现分页功能的具体代码,供大家参考,具体内容如下

不用根据改变SQL的形式去查询;
直接查询所有的数据,根据页码自动显示数据;

分页对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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;
  }
 
}

查询的数据实体

在查询的实体里添加页码和每页显示条数参数;

?
1
2
3
4
5
6
7
8
private int pageSize;  //每页显示的条数
private int pageNo;   //当前页码
public int getPageSize() {
  return pageSize;
}
public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
}

控制层Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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";
  }

页面处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//循环穿过来的PAGE.data数据
<tr th:each="ts : ${page.data}">
<td th:text="${ts.channelValueName}"></td>
 
 
----------
<form id="content_form" action="/channelValue/list" method="post" >
  <div>
    总数:<span id="totalCount" th:text="${page.totalCount}">0</span>
  </div>
  <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()">&lt;</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()">&gt;</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($('#beginRow').html());
    if (beginRow - 1 > 0) {
      onList(beginRow - 1);
    }
 
  }
  function onNext() {
    var beginRow = parseInt($('#beginRow').html());
    var totalCount = parseInt($('#totalCount').html());
    var pageSize = parseInt($('#pageSize').val());
    if (parseInt(totalCount / pageSize + 1) > beginRow + 1) {
      onList(beginRow+1);
    }
  }
  function onLast() {
    var totalCount = parseInt($('#totalCount').html());
    var pageSize = parseInt($('#pageSize').val());
    onList(parseInt(totalCount / pageSize + 1) - 1);
  }
  function onList(pageNo) {
    if (pageNo == 0)
      pageNo = 1;
    $('#pageNo').val(pageNo);
    $("#content_form").submit();
  }
</script>

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

延伸 · 阅读

精彩推荐