---
title: "Hibernate范型-离线分页实现（上）"
author: "mercyblitz"
date: "2007-06-07T21:21:00Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2007-06-07T21:21:00Z"
id: 87746
url: "https://mercyblitz.iteye.com/blog/87746"
---

我们现在学习的是基于Hibernate的离线查询分页，我们在Web设计中可以选用Struts框架、JSTL等等来实现其功能。&lt;o:p&gt;&lt;/o:p&gt;

首先，我们要理解分页的算法，也要讲究效率，也就是说我们不能把所有的记录取出来，我们只取一个页面中需要显示的纪录，那么怎么去实现是我们关心的问题！不要着急，重在理解而是单纯技术实现。我采用Web框架两种方式来实现，attention!&lt;o:p&gt;&lt;/o:p&gt;

分页算法，也就是我们提供给客户方便的操作。一般有哪些东西呢？&lt;o:p&gt;&lt;/o:p&gt;

       上页、下页、首页、末页、导航到X页，页码导航。说到这里我们先放下，我们来说说取出页面的记录，也就是只取一段的记录。打开Hibernate API 看到Criteria 接口，public Criteria setMaxResults(int maxResults)，意思为：设置记录对象数目的上限，第二方法,public Criteria setFirstResult setFirstResult(int firstResult),意思：设置开始取得记录对象的下标，其中的第一对象下标为0。这样我们可以看出Hibernate提供很好的方法给我们实现分页。&lt;o:p&gt;&lt;/o:p&gt;

       我们产生了思路：我们只有设置firstResult得下标，我们这里把它叫做游标。我们只要控制游标就可以控制取得的记录就是我们想要得到的，下面要做的工作就是我们来控制index,下面我写了一个HPage的类来实现这个工作，也就是取得一些相关的值，其中我们关系的有：当前的页码、当前的游标等等，下面我们结合代码来说明下！~~~&lt;o:p&gt;&lt;/o:p&gt;

实现代码如下：

java 代码

001. public class HPage {
002. private int countPage;  //页面的数量
003. private int startIndex=0; //纪录游标
004. private int recordPerPage;  //每页中的记录数量
005. private int pageIndex=1;  //初始的页码
006. private boolean nextPage=true;
007. private boolean previousPage=false;
008. private boolean firstPage=true;
009. private boolean lastPage=false;
010. public int getCountPage()  //取得页面的数量
011. {
012. return countPage;
013. }
014. public int getStartIndex() //取得游标的位置
015. {
016. return startIndex;
017. }
018. public HPage(int countRecord,int recordPerPage)
019. //coutRecord参数：纪录的总数量；recordPerPage参数：设置每页的记录数量
020. {
021. this.recordPerPage=recordPerPage;
022. if(countRecord % recordPerPage == 0)
023. countPage=countRecord / recordPerPage;
024. else
025. countPage=countRecord / recordPerPage +1;
026. }
027. public int getPageIndex() // 取得当前页码
028. {
029. return pageIndex;
030. }
031. public int nextPage() // 游标下一页滚动
032. {
033. pageIndex++;
034. if(isOnlyOne())
035. {
036. nextPage=false;
037. previousPage=false;
038. firstPage=false;
039. lastPage=false;
040. }
041. else
042. {
043. previousPage=true;
044. firstPage=true;
045. }
046. if(pageIndex &gt; countPage)
047. return lastPage();
048. else
049. {
050. return (startIndex=forwardPage(pageIndex));
051. }
052. }
053. public int previousPage()  // 游标上一页滚动
054. {
055. pageIndex--;
056. if(isOnlyOne())
057. {
058. nextPage=false;
059. previousPage=false;
060. firstPage=false;
061. lastPage=false;
062. }
063. else
064. {
065. nextPage=true;
066. lastPage=true;
067. }
068. if(pageIndex &lt;= 1)
069. return firstPage();
070. else
071. {
072. return (startIndex=forwardPage(pageIndex));
073. }
074. }
075. public int lastPage() // 游标向末页滚动
076. {
077. pageIndex=countPage;
078. if(isOnlyOne())
079. {
080. nextPage=false;
081. previousPage=false;
082. firstPage=false;
083. lastPage=false;
084. }
085. else
086. {
087. nextPage=false;
088. previousPage=true;
089. firstPage=true;
090. lastPage=false;
091. }
092. return (startIndex=(countPage-1) * recordPerPage);
093. }
094. public int firstPage() // 游标向首页滚动
095. {
096. pageIndex=1;
097. if(isOnlyOne())
098. {
099. nextPage=false;
100. previousPage=false;
101. firstPage=false;
102. lastPage=false;
103. }
104. else
105. {
106. nextPage=true;
107. previousPage=false;
108. firstPage=false;
109. lastPage=true;
110. }
111. return (startIndex=0);
112. }
113. public int forwardPage(int pageIndex) //导向制定页面
114. {
115. if(pageIndex &gt;= countPage)
116. {
117. pageIndex=countPage;
118. return (startIndex=lastPage());
119. }
120. else if(pageIndex &lt;= 1)
121. {
122. pageIndex=1;
123. return startIndex=firstPage();
124. }
125. this.pageIndex=pageIndex;
126. if(isOnlyOne())
127. {
128. nextPage=false;
129. previousPage=false;
130. firstPage=false;
131. lastPage=false;
132. }
133. else
134. {
135. nextPage=true;
136. previousPage=true;
137. firstPage=true;
138. lastPage=true;
139. }
140. return (startIndex=(this.pageIndex -1)* recordPerPage);
141. }
142. private boolean isOnlyOne() //是否只有一页
143. {
144. return countPage==1 ? true : false;
145. }
146. public boolean isNextPage() //是否有下页
147. {
148. return nextPage;
149. }
150. public boolean isPreviousPage() //是否有上页
151. {
152. return previousPage;
153. }
154. public boolean isFirstPage() //是否首页
155. {
156. return firstPage;
157. }
158. public boolean isLastPage() //是否末页
159. {
160. return lastPage;
161. }
162. }

 

不是很难吧！好的，现在我们做好的工作就是取得了我们要的数值，下面要做的是加到Hibernate查询里面去，下面的工作很简单。但是我们要注意几个问题，第一个问题，我们的设计的目的不是为了解决一个问题，而是抽象出一个方案提供给相似的问题的解决方案。这样的话，我们最大化重利用代码，那么我们采用范性的特点（注意JDK5.0的特征，以前的版本会报错误）。下面我分析一下代码：&lt;o:p&gt;&lt;/o:p&gt;

java 代码

001. public class DisplayPageRecordList&lt;t&gt;&lt;/t&gt; { //泛型实现
002. private int startIndex;
003. private static final int DEFAULT\_RECORD\_PER\_PAGE=5; //默认：每页中5条记录
004. private List&lt;t&gt;&lt;/t&gt; list;
005. private HPage page;
006. private int recordPerPage=DEFAULT\_RECORD\_PER\_PAGE;
007. private final Session session=HibernateSessionFactory.getSession();
008. private DetachedCriteria deCriteria;
009. private Criteria criteria;
010. private int countRecord;
011. public DisplayPageRecordList(String className) throws ClassNotFoundException
012. //className参数：类的全定名称
013. {
014. deCriteria=DetachedCriteria.forClass(Class.forName(className)); //创造离线的查询
015. criteria=deCriteria.getExecutableCriteria(session);
016. criteria.setCacheable(true);
017. doInit();
018. page=new HPage(countRecord,recordPerPage);
019. }
020. public DisplayPageRecordList(int recordPerPage,String className) throws ClassNotFoundException
021. //recordPerPage参数：每页的纪录的条目数，className参数：同上个构造方法中
022. {
023. deCriteria=DetachedCriteria.forClass(Class.forName(className));
024. criteria=deCriteria.getExecutableCriteria(session);
025. criteria.setCacheable(true);
026. this.recordPerPage=recordPerPage;
027. doInit();
028. page=new HPage(countRecord,recordPerPage);
029. }
030. public DisplayPageRecordList(int recordPerPage,String className,Criterion criterion) throws ClassNotFoundException
031. //recordPerPage参数：同上个构造方法中，className参数：同上个构造方法中，criterion参数：设置查询条件
032. {
033. deCriteria=DetachedCriteria.forClass(Class.forName(className));
034. criteria=deCriteria.getExecutableCriteria(session);
035. criteria.add(criterion);
036. criteria.setCacheable(true);
037. doInit();
038. page=new HPage(countRecord,recordPerPage);
039. }
040. public void setRecordPerPage(int recordPerPage)
041. {
042. this.recordPerPage=recordPerPage;
043. page=new HPage(countRecord,recordPerPage);
044. }
045. private void doInit()
046. {
047. this.countRecord=criteria.list().size(); //计算数据库中所有的记录数
048. }
049. public void addCriterion(Criterion criterion)
050. {
051. criteria=criteria.add(criterion); //更改所有所有查询条件
052. doInit();
053. }
054. public void setCacheable(boolean cache) //是否启用缓存
055. {
056. criteria.setCacheable(cache);
057. }
058. public List doNextPage() //查询出下页中的纪录集
059. {
060. startIndex=page.nextPage();
061. criteria.setFirstResult(startIndex);
062. criteria.setMaxResults(this.recordPerPage);
063. list=criteria.list();
064. return list;
065. }
066. public List doPreviousPage() //查询出上页中的纪录集
067. {
068. startIndex=page.previousPage();
069. criteria.setFirstResult(startIndex);
070. criteria.setMaxResults(this.recordPerPage);
071. list=criteria.list();
072. return list;
073. }
074. public List doFirstPage() //查询出首页中的纪录集
075. {
076. startIndex=page.firstPage();
077. criteria.setFirstResult(startIndex);
078. criteria.setMaxResults(this.recordPerPage);
079. list=criteria.list();
080. return list;
081. }
082. public List doLastPage() //查询出末页中的纪录集
083. {
084. startIndex=page.lastPage();
085. criteria.setFirstResult(startIndex);
086. criteria.setMaxResults(this.recordPerPage);
087. list=criteria.list();
088. return list;
089. }
090. public List doForward(int pageIndex)
091. {
092. startIndex=page.forwardPage(pageIndex);
093. criteria.setFirstResult(startIndex);
094. criteria.setMaxResults(this.recordPerPage);
095. list=criteria.list();
096. return list;
097. }
098. public HPage getPage() //取得页面对象
099. {
100. return this.page;
101. }
102. }

 

以上代码中，实现了泛型-离线查询的页面的代码。上篇完，下篇我们来使用Struts和JSTL在网页上测试&lt;o:p&gt;&lt;/o:p&gt;

 

&lt;o:p&gt;&lt;/o:p&gt;