Java實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)功能
本文實(shí)例為大家分享了Java實(shí)現(xiàn)分頁(yè)功能的具體代碼,供大家參考,具體內(nèi)容如下
不用根據(jù)改變SQL的形式去查詢;
直接查詢所有的數(shù)據(jù),根據(jù)頁(yè)碼自動(dòng)顯示數(shù)據(jù);
分頁(yè)對(duì)象
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;//每頁(yè)顯示記錄數(shù)
private long firstResult=0;//當(dāng)頁(yè)第一條記錄號(hào)
private long totalCount;//總記錄數(shù)
private long totalPage;//總頁(yè)碼
private long pageNo=1;//當(dāng)前頁(yè)碼
private List<?> sumData;//此集合可用來(lái)保存 合計(jì)數(shù)據(jù)
private List<?> data;//查詢結(jié)果
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;
}
/**
* 是否第一頁(yè)
*/
public boolean isFirstPage() {
return pageNo <= 1;
}
/**
* 是否最后一頁(yè)
*/
public boolean isLastPage() {
return pageNo >= getTotalPage();
}
/**
* 下一頁(yè)頁(yè)碼
*/
public long getNextPage() {
if (isLastPage()) {
return pageNo;
} else {
return pageNo + 1;
}
}
/**
* 上一頁(yè)頁(yè)碼
*/
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;
}
}
查詢的數(shù)據(jù)實(shí)體
在查詢的實(shí)體里添加頁(yè)碼和每頁(yè)顯示條數(shù)參數(shù);
private int pageSize; //每頁(yè)顯示的條數(shù)
private int pageNo; //當(dāng)前頁(yè)碼
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
控制層Controller
@RequestMapping("/list")
public String list(Model model,ChannelValueInfoView input) {
// input:傳入的參數(shù)為對(duì)象
PageUtils page=new PageUtils();
//如果傳入的當(dāng)前條數(shù)為0,則賦予值(首次查詢不帶參);
if(input.getPageSize()==0){
//當(dāng)前頁(yè)碼第一頁(yè)
input.setPageNo(1);
//每頁(yè)顯示條數(shù),當(dāng)前每頁(yè)顯示10條數(shù)據(jù);
input.setPageSize(10);
}
page.setPageNo(input.getPageNo());
page.setPageSize(input.getPageSize());
//核心分頁(yè)代碼
PageHelper p=new PageHelper();
Page<ChannelValueInfoList> l=p.startPage(input.getPageNo(),input.getPageSize());
//緊跟著的第一個(gè)select查詢將會(huì)被分頁(yè)
channelValueService.getChannelValueInfoViewList(input);
model.addAttribute("input", input);
page.setData(l);
page.setTotalCount(l.getTotal());
model.addAttribute("page", page);
return "index";
}
頁(yè)面處理
//循環(huán)穿過(guò)來(lái)的PAGE.data數(shù)據(jù)
<tr th:each="ts : ${page.data}">
<td th:text="${ts.channelValueName}"></td>
----------
<form id="content_form" action="/channelValue/list" method="post" >
<div>
總數(shù):<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()">首頁(yè)</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()">尾頁(yè)</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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用ThreadLocal實(shí)現(xiàn)當(dāng)前登錄信息的存取功能
ThreadLocal和其他并發(fā)工具一樣,也是用于解決多線程并發(fā)訪問(wèn),下這篇文章主要給大家介紹了關(guān)于Java使用ThreadLocal實(shí)現(xiàn)當(dāng)前登錄信息的存取功能,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Spring Validator接口校驗(yàn)與全局異常處理器
SpringBoot?整合?Spring-Session?實(shí)現(xiàn)分布式會(huì)話項(xiàng)目實(shí)戰(zhàn)

