国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
spring分頁匯總
userphoto

2010.08.18

關注
對于分頁,主要思想無非兩種,一是從數(shù)據(jù)庫取出所有記錄后進行分頁,另一種思路是在取出數(shù)據(jù)的同時進行分頁,然后在頁面顯示,昨晚查了不少資料,對目前流行的分頁方式總結了下。
個人覺得寫的比較好的兩篇文章:
http://www.matrix.org.cn/thread.shtml?topicId=40818&forumId=23
http://forum.javaeye.com/viewtopic.php?t=6298
(java視線對這個問題討論的比較深入,受益匪淺)
本人目前有個項目要用到Spring的JdbcTemplate,順帶搜了點關于其分頁的實現(xiàn)方法:
                                            用Spring的JdbcTemplate實現(xiàn)分頁功能
最近使用了spring中的JdbcTemplate實現(xiàn)數(shù)據(jù)庫的查詢和插入操作,發(fā)現(xiàn)spring的JdbcTemplate不象HibernateTemplate那么好,已經(jīng)實現(xiàn)了分頁功能。所以要自己實現(xiàn),使用getJdbcTemplate().queryForList(string sql)得到的結果集是所有的。

如果你的查詢有10000條記錄,或者更多,速度肯定慢了,當然你可以通過resultset中的游標控制查詢的起始和結束。我這里用的是Oracle數(shù)據(jù)庫,使用偽列ROWNUM來實現(xiàn)分頁。我的分頁代碼如下:


    package com.deity.ranking.util;import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/** * 分頁函數(shù) * * @author allenpan */public class Pagination extends JdbcDaoSupport{
public static final int NUMBERS_PER_PAGE = 10;
//一頁顯示的記錄數(shù)
private int numPerPage;
//記錄總數(shù)
private int totalRows;
//總頁數(shù)
private int totalPages;
//當前頁碼
private int currentPage;
//起始行數(shù)
private int startIndex;
//結束行數(shù)
private int lastIndex;
//結果集存放List
private List resultList;
//JdbcTemplate jTemplate
private JdbcTemplate jTemplate;
/**
* 每頁顯示10條記錄的構造函數(shù),使用該函數(shù)必須先給Pagination設置currentPage,jTemplate初值
* @param sql oracle語句
*/
public Pagination(String sql){
if(jTemplate == null){
throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
}else if(sql.equals("")){
throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
}
new Pagination(sql,currentPage,NUMBERS_PER_PAGE,jTemplate);
}
/**分頁構造函數(shù)
* @param sql 根據(jù)傳入的sql語句得到一些基本分頁信息
* @param currentPage 當前頁
* @param numPerPage 每頁記錄數(shù)
* @param jTemplate JdbcTemplate實例
*/
public Pagination(String sql,int currentPage,int numPerPage,JdbcTemplate jTemplate){
if(jTemplate == null){
throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
}else if(sql == null || sql.equals("")){
throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
}
//設置每頁顯示記錄數(shù)
setNumPerPage(numPerPage);
//設置要顯示的頁數(shù)
setCurrentPage(currentPage);
//計算總記錄數(shù)
StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
totalSQL.append(sql);
totalSQL.append(" ) totalTable ");
//給JdbcTemplate賦值
setJdbcTemplate(jTemplate);
//總記錄數(shù)
setTotalRows(getJdbcTemplate().queryForInt(totalSQL.toString()));
//計算總頁數(shù)
setTotalPages();
//計算起始行數(shù)
setStartIndex();
//計算結束行數(shù)
setLastIndex();
System.out.println("lastIndex="+lastIndex);//////////////////
//構造oracle數(shù)據(jù)庫的分頁語句
StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
paginationSQL.append(sql);
paginationSQL.append(" ) temp where ROWNUM  " + startIndex);
//裝入結果集
setResultList(getJdbcTemplate().queryForList(paginationSQL.toString()));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub    }
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getNumPerPage() {
return numPerPage;
}
public void setNumPerPage(int numPerPage) {
this.numPerPage = numPerPage;
}
public List getResultList() {
return resultList;    }
public void setResultList(List resultList) {
this.resultList = resultList;
}
public int getTotalPages() {
return totalPages;
}
//計算總頁數(shù)
public void setTotalPages() {
if(totalRows % numPerPage == 0){
this.totalPages = totalRows / numPerPage;
}else{
this.totalPages = (totalRows / numPerPage) + 1;
}
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex() {
this.startIndex = (currentPage - 1) * numPerPage;
}
public int getLastIndex() {
return lastIndex;
}
public JdbcTemplate getJTemplate() {
return jTemplate;
}
public void setJTemplate(JdbcTemplate template) {
jTemplate = template;
}
//計算結束時候的索引
public void setLastIndex() {
System.out.println("totalRows="+totalRows);///////////
System.out.println("numPerPage="+numPerPage);///////////
if( totalRows  1970 && rankMonth > 0){
//hql.append(" and sas.id.dt >= to_date('" + rankYear + "-" + rankMonth + "-01 00:00:00'," + "YYYY-MM-DD HH24:MI:SS");
//hql.append(" and sas.id.dt 非常簡單但有使用的分頁條件判斷語句。
if ($page 首頁][上一頁]";
        }
        if ($page >= $page_total) {
            $page_str .= "[下一頁][尾頁]";
        } else {
            $page_str .= "[下一頁][尾頁]";
        } 
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Struts Hibernate開發(fā)實踐 分頁的實現(xiàn)-lude707 -JavaEye技術...
Jsp中分頁功能的實現(xiàn)
WEB開發(fā)中Struts分頁算法
Hibernate實現(xiàn)分頁查詢
springJdbc分頁--mysql
用jQuery和jTemplates插件實現(xiàn)客戶端分頁的表格展現(xiàn)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服