package common.util;
在紅旗等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,紅旗網(wǎng)站建設(shè)費(fèi)用合理。
import java.util.*;
public class PageController implements IPageModel {
private Collection model;
//數(shù)據(jù)總行數(shù)
private int totalRowCount = 0; //
//總頁數(shù)
private int pageCount = 0;
//每頁應(yīng)顯示的行數(shù)
private int maxPageRowCount = 0;
//當(dāng)前頁行數(shù)
private int currPageRowCount = 0;
//當(dāng)前頁號(hào)
private int currPageNum;
//默認(rèn)構(gòu)造
public PageController() {
super();
}
//傳入模型
public PageController(Collection model) {
setPageController(model);
}
//設(shè)一個(gè)分頁模型
public void setPageController(Collection model) {
this.model = model;
this.totalRowCount = model.size();
}
/**
* 總頁數(shù)
* @return int
*/
public int getPageCount() {
return this.pageCount;
}
/**
* getPageContents
*
* @param intPageNum int
* @return Object
*/
public Object getPageContents(int intPageNum) {
//非法數(shù)據(jù)
if(intPageNum1){
intPageNum=1;
}
if(intPageNumpageCount){
intPageNum=pageCount;
}
//指定當(dāng)前頁
this.currPageNum=intPageNum;
int i = 0;
ArrayList arr = new ArrayList();
//如果是合法的范圍
if (intPageNum 0 intPageNum = pageCount) {
//計(jì)算該頁的開始號(hào)和結(jié)束號(hào)
int lfromrow = (intPageNum - 1) * maxPageRowCount;
arr = (ArrayList) getElementsAt(model, lfromrow, lfromrow + maxPageRowCount-1);
}
currPageNum=intPageNum;
return arr;
}
public Object getLastPage() {
return this.getPageContents(pageCount);
}
public Object getFirstPage() {
return this.getPageContents(0);
}
/**
* getCurrentPageRowsCount
*
* @return int
*/
public int getCurrentPageRowsCount() {
if(currPageNumpageCount){
return maxPageRowCount;
}
else{//最后一頁
return totalRowCount-(pageCount-1)*maxPageRowCount;
}
}
public int getCurrentPageNum(){
return currPageNum;
}
/**
* setMaxPageRows
*
* @return int
*/
public void setMaxPageRows(int rowCount) {
maxPageRowCount = rowCount;
//計(jì)算總頁數(shù)
if (totalRowCount % maxPageRowCount 0) { //有余數(shù)
pageCount = totalRowCount / maxPageRowCount + 1;
}
else {
pageCount = totalRowCount / maxPageRowCount;
}
}
/**
* getMaxPageRows
*/
public int getMaxPageRows() {
return maxPageRowCount;
}
//私有方法,返回集合中指定范圍的數(shù)據(jù)
private Object getElementsAt(Collection model, int fromIndex, int toIndex) {
Iterator iter = model.iterator();
ArrayList arr = new ArrayList();
if (iter != null) {
int i = 0;
while (iter.hasNext()) {
Object obj=iter.next();
if (i = fromIndex i = toIndex) {
arr.add(obj);
}
if (i toIndex) {
break;
}
i = i + 1;
}
}
return arr;
}
}
分頁顯示一般有兩種實(shí)現(xiàn)方式:業(yè)務(wù)層分頁、數(shù)據(jù)庫層分頁(以下會(huì)用到兩個(gè)參數(shù),提前說明下 page:請(qǐng)求第幾頁,size:每頁顯示多少條)
業(yè)務(wù)層分頁:從數(shù)據(jù)庫取出所有數(shù)據(jù),然后通過傳過來的page和size對(duì)所有數(shù)據(jù)截取,比如一共查了100條數(shù)據(jù),保存在list里面,要求查詢第2頁,每頁顯示10條,則可以通過list屬性,取100條數(shù)據(jù) 中的第11條到第20條,可通過遍歷實(shí)現(xiàn)。
數(shù)據(jù)庫層分頁:數(shù)據(jù)庫都會(huì)有分頁函數(shù)(mysql 是limit函數(shù),sqlServer是row_number()函數(shù),可自行百度下)該方法是通過傳過來的page和size在查詢數(shù)據(jù)庫時(shí)就開始分頁,以mysql為例,查詢第2頁,每頁顯示10條,則sql語句是 ”select * from XX limit 10,10“(第一個(gè)10表示從下標(biāo)為10開始查,第二個(gè)10是共讀取10條)
性能肯定是第二種分頁方式好,只要搞懂分頁原理,想實(shí)現(xiàn)分頁其實(shí)很簡單,只要搞清楚分頁是將多條數(shù)據(jù)中的某幾條挑出來
Java
Java是一種可以撰寫跨平臺(tái)應(yīng)用軟件的面向?qū)ο蟮某绦蛟O(shè)計(jì)語言。
intPageCount = (intRowCount+intPageSize-1) / intPageSize;等價(jià)于
intPageCount=(intRowCount-1/intPageSiez)+1;
如果introwCount(總記錄)=8,intPageSize(每頁記錄)=8則共為1頁,如果rowCount=14,inPageSize=8則為2頁,就是說總頁數(shù)至少是要比8的倍數(shù)多1才會(huì)多翻一頁.你想想如果一頁顯示八個(gè)記錄,而總數(shù)剛好最后頁有8個(gè)記錄,你會(huì)翻頁嗎,不會(huì)的吧!!就這個(gè)道理
其實(shí)可以用為intPageCount=intRowCount%intPageSize==0?(intRowCount)/intPageSize:(intRowCount/intPageSize+1);
假設(shè)rowCount(總記錄數(shù))=8 intPageSize(每頁記錄數(shù))=8,那么總頁數(shù)就是因?yàn)?%8==0則8/8=1
就是一頁
假設(shè)rowCount=14 intPageSize=8則總頁數(shù)就是因?yàn)?4%8!=0則14/8+1=2
就是兩頁這樣分析樓主不知道能否明白
通過rs.next()進(jìn)行分頁,首先用rs.absolute(0)定位到某條記錄。然后循環(huán)取數(shù)例如:1-10 可以for(int i=0;i10;i++){rs.next()}
package dl.wsxx.base;
public class Pager {
private int totalRows; // 總行數(shù)
private int pageSize; // 每頁顯示的行數(shù)
private int currentPage; // 當(dāng)前頁號(hào)
private int totalPages; // 總頁數(shù)
private int startRow; // 當(dāng)前頁在數(shù)據(jù)庫中的起始行
private int pageStartRow; // 當(dāng)前頁開始行
private int pageEndRow; // 當(dāng)前頁結(jié)束行
private int hasNextPage; // 下一頁存在標(biāo)識(shí)[0:不存在,1:存在]
private int hasPreviousPage; // 前一頁存在標(biāo)識(shí)[0:不存在,1:存在]
public Pager() {
}
public Pager(int _totalRows,int _pageSize) {
pageSize = _pageSize;
totalRows = _totalRows;
totalPages = totalRows / pageSize;
int mod = totalRows % pageSize;
if (mod 0) {
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getpageStartRow() {
return pageStartRow;
}
public int getpageEndRow() {
return pageEndRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public int getHasNextPage() {
return hasNextPage;
}
public int getHasPreviousPage() {
return hasPreviousPage;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setHasNextPage(int hasNextPage) {
this.hasNextPage = hasNextPage;
}
public void setHasPreviousPage(int hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 0;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void next() {
if (currentPage totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage totalPages) {
last();
}
this.hasFlagSet(currentPage, totalPages);
}
private void hasFlagSet(int currentPage, int totalPages) {
if (currentPage == totalPages) {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 0;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 0;
}
} else {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 1;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 1;
}
}
}
}
這是我的工程里的分頁核心代碼,希望對(duì)你有用,還有ssh分頁文檔,可以參照研究一下。