国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

java 簡潔的分層實現(xiàn)

2019-11-15 00:36:20
字體:
供稿:網(wǎng)友
java 簡潔的分層實現(xiàn)

1.分頁實現(xiàn)

分頁實現(xiàn)是將所有查詢結(jié)果保存在session對象或集合中,翻頁時從session對象或集合中取出一頁所需的數(shù)據(jù)顯示。但是這種方法有兩個最主要的缺點:一是用戶看到的可能是過期數(shù)據(jù);二是如果數(shù)據(jù)量非常大,查詢一次數(shù)據(jù)集會耗費很長時間,并且存儲的數(shù)據(jù)也會占用大量內(nèi)存,效果明顯下降。

2.實現(xiàn)數(shù)據(jù)分頁顯示的步驟

(1)確定每頁顯示的數(shù)據(jù)數(shù)量。根據(jù)頁面的設(shè)計,確定在數(shù)據(jù)列表中每次顯示多少條記錄,也就是說每次從數(shù)據(jù)庫中需要查詢出多少條記錄用于頁面顯示,通常這個數(shù)量可以在開發(fā)時定義好,也可以通過用戶來確定。

(2)計算顯示的總頁數(shù)。既然要進(jìn)行分頁顯示,還需清楚按照每頁顯示的數(shù)據(jù)庫記錄數(shù)量,計算出需要劃分的總頁數(shù)。由于在頁面中顯示的記錄數(shù)量是固定的,而數(shù)據(jù)庫中共存儲了多少條記錄是未知的,因此要想得到總頁數(shù),需要以下步驟:

<1>首先要通過查詢獲取數(shù)據(jù)庫中總的記錄數(shù),在SQL Server 數(shù)據(jù)庫中提供了count()聚合函數(shù),借助count()聚合函數(shù)就可以獲取數(shù)據(jù)庫中記錄的總數(shù),代碼如下所示:

定義新聞接口代碼如下。

public interface NewsDao{

public int getTotalCount();

}

新聞接口的實現(xiàn)類中獲取數(shù)據(jù)庫中記錄總數(shù)的代碼如下。

public class NewsDaoiple extends BaseDao implements NewsDao{

public int getTotalCount(){

int count =0;

String sql="select count(*) fro News";

              ......//省略執(zhí)行代碼

              if(rs.next()){

              count=rs.getInt(1);

}

....//省略執(zhí)行代碼

               returncount

}

}

從代碼中可以看出,當(dāng)你執(zhí)行使用了count()函數(shù)的SQL語句后,將獲得news表中的記錄總數(shù),然后將其數(shù)據(jù)返回。

<2>有了數(shù)據(jù)庫記錄總數(shù)后,就可以根據(jù)每頁顯示的記錄數(shù)計算共需要劃分為多少頁,將有關(guān)分頁的數(shù)據(jù)封裝到page類,代碼如下:

      public class Page{

         //總頁數(shù)

         PRivate int totalPageCount=1;

//頁面大小,即每頁顯示記錄數(shù)

         private int pageSize=0;

//記錄總數(shù)

private int totalCount=0;

         //當(dāng)前頁碼

         private int currPageNo=1;

         //每頁新聞集合

List<News> newsList;

.........//省略其他getter/setter()方法

         public int getCurrPageNo(){

if(totalPageCount==0)

                return 0;

return currPageNo;

          }

public void setCurrPageNo(int currPageNo){

            if(this.currPageNo>0)

               this.currPageNo = currPageNo;

}

public int getPageSize(){

return pageSize;

}

public void setPageSize(int pageSize){

if(pageSize>0)

this.pageSize = pageSize;

}

         public int getTotalCount(){

            return totalCount;

}

         public void setTotalCount(int totalCount){

             if(totalCount>0){

              this.totalCount = totalCount;

//計算總頁數(shù)

totalPageCount=this.totalCount%pageSize==0?(this.totalCount/pageSize) : this.totalCount/pageSize+1;

}

}

}

以上代碼設(shè)置記錄總數(shù)的setTotalCount()方法中,根據(jù)記錄總數(shù)和每頁顯示記錄數(shù)通過公式計算出總頁數(shù),其使用了條件三元運算符 "? :" 的方式進(jìn)行數(shù)據(jù)處理。如果記錄總數(shù)能被每頁顯示記錄數(shù)整除,則總頁數(shù)為兩者的商;如果不能被整除,則余出的記錄數(shù)單獨列為一頁,所以總頁數(shù)為兩者的商再加一。

  (3)編寫SQL語句。

《1》假如每頁顯示3條記錄,若要顯示第一頁的記錄,則:

String sql="select top 3 NID,NTITLE,NCREATEDATE from News"+" where NID not in (select top 0 NID from News)";

上述語句中top作用是限制返回的行數(shù),此語句運用了兩層嵌套的查詢方式,內(nèi)層的select語句是一條普通的返回限制行的查詢語句,他的執(zhí)行結(jié)果實際上是為外層的select語句起到一個限制范圍的作用,其中數(shù)字0是起始行的下標(biāo),如顯示第一頁則從第一行開始查詢,即起始下標(biāo)為0.而外層的select語句限制的行數(shù)實際上是每頁要顯示的記錄數(shù),其執(zhí)行結(jié)果就是從內(nèi)層語句的查詢結(jié)果中按照起始行的下標(biāo)取出前三條(每頁顯示的記錄數(shù))。

起始行的下標(biāo) = (當(dāng)前頁頁碼-1)* 每頁顯示的數(shù)據(jù)量

《2》String sql="select top "+pageSize+" NID,NTITLE,NCREATEDATE from News"+"where NID not in(select top "+(currPageNo-1)* pageSize+"NID from News )";

pageSize 表示每頁顯示的記錄數(shù),而currPageNo 變量表示當(dāng)前頁的代碼。

定義新聞接口的代碼如下:

    public interface NewsDao{

      public int getTotalCount();

      public List<News> getPageNewsList(int pageNo,int pageSize);

}

新聞接口的實現(xiàn)類中獲得每頁新聞集合代碼如下:

public class NewsDaoImple extends BaseDao implements NewsDao{

.....//省略查詢數(shù)據(jù)庫中記錄總數(shù)的代碼

public List<News> getPageNewsList(int pageNo,int pageSize){

          List<News> newslist=new ArrayList<News>();

Connection con=this.getConnection();

          PreparedStatement ps=null;

ResultSet rs=null;

String sql="select top "+pageSize+"NID,NTITLE,NCREATEDATE from News where NID not in "+"(select top "+(pageNo-1)*pageSize+"NID from News"+"order by NCREATEDATE desc) order by NCREATEDATE desc";

.......//省略其他執(zhí)行代碼

return newslist;

}

}

向頁面顯示信息:

public class NewsDAoImple extends BaseDao implements NewsDao{

..........//省略其他代碼實現(xiàn)

public static void main(String[] args){

NewsDaoImple news =new NewsDaoImple();

int totalCount=news.getTotalCount();

              Page page =new Page();

page.setCurrPageNo(1); //設(shè)置當(dāng)前頁面

              page.setPageSize(3); //設(shè)置每頁條數(shù)

              page.setTotalCount(totalCount); //設(shè)置總數(shù)量

System.out.println("新聞總數(shù)量是:"+page.getTotalCount());

              System.out.println("每頁條數(shù)是:" +page.setPageSize(););

              System.out.println("總頁數(shù):"page.getTotalPageCount());

              System.out.println("當(dāng)前是第"+page.getCurrPageNo()"頁");

List<News> newslist=news.getPageNewsList(page.getCurrPageNo(), page.getPageSize());

              page.setListNews(newslist);

              for(News news:page.getListNews()){

                System.out.println(news.getNid()+"/t"+news.getNtitle()+"/t"+news.getNcreatedate());

}

}

}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 余江县| 江口县| 铜山县| 浦北县| 石楼县| 遵义县| 哈尔滨市| 壤塘县| 吐鲁番市| 秦安县| 玉环县| 兰溪市| 卢氏县| 新干县| 民权县| 尚志市| 京山县| 衢州市| 惠东县| 客服| 阜新市| 行唐县| 延津县| 营口市| 兴山县| 扎囊县| 旺苍县| 高陵县| 南投市| 长岭县| 无极县| 开鲁县| 普兰县| 涞源县| 舞阳县| 玛多县| 牡丹江市| 青州市| 吴堡县| 定结县| 双柏县|