DividePageDAO.class
package com.fiona.util.dividePageUtile;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class DividePageDAO extends HibernateDaoSupport {
public static final Log log = LogFactory.getLog(DividePageDAO.class);
@SuppressWarnings("unchecked")
public List findByPageSize(final int startRecord,
final int pageSize,Class className)
{
try
{
log.debug("findByPageSize::start...");
final DetachedCriteria crit = DetachedCriteria.forClass(className);
List list=(List) getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(org.hibernate.Session session)
throws HibernateException, SQLException {
Criteria c1 = crit.getExecutableCriteria(session);
List item = c1.setFirstResult(startRecord).setMaxResults(pageSize).list();
log.debug("the size of the item is " + item.size());
return item;
}
});
return list;
}
catch(RuntimeException e)
{
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
public Integer findTotalCount(Class className)
{
log.debug("findTotalCount::start...");
try
{
final DetachedCriteria crit = DetachedCriteria.forClass(className);
Integer totalCount = (Integer)getHibernateTemplate().execute(
new HibernateCallback()
{
public Object doInHibernate(Session session)
throws HibernateException, SQLException
{
Criteria ct = crit.getExecutableCriteria(session);
return ct.setProjection(Projections.rowCount()).uniqueResult();
}
});
log.debug("the tottla count is " + totalCount);
return totalCount.intValue();
}
catch(RuntimeException e)
{
e.printStackTrace();
return 0;
}
}
}
Flex端的组件
<?xml version="1.0" encoding="utf-8"?>
<mx:ControlBar xmlns:mx="http://www.adobe.com/2006/mxml"
height="40" width="100%" horizontalAlign="center">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
public var pageSize:int = 25;//每页显示行数
public var _totalPage:int = 0;//总页数
public var _totalRecord:int = 0; //总记录数
public var curPage:int = 1; // 当前页
public var startRecord:int = 0; // 起始行
public var endRecord:int; // 当前页的结束行
public var acPageSize:ArrayCollection = new ArrayCollection([
{data:1,label:"25"},{data:2,label:"50"}]);
public var _genFunction:Function;
public function set totalRecord(varTotal:int):void
{
this._totalRecord = varTotal;
this.computeThePageSize(this._totalRecord,this.pageSize);
}
public function get totalRecord():int
{
return this._totalRecord;
}
public function set functionName(varFunction:Function):void
{
this._genFunction = varFunction;
}
public function get functionName():Function
{
return this._genFunction;
}
public function computeThePageSize(varTotalRecore:int,varPageSize:int):void
{
if ( varTotalRecore == 0 )
{
_totalPage = 0;
this.jumpPage.minimum = 1;
this.jumpPage.maximum = 1;
Alert.show("没有任何学生信息");
}
else
{
_totalPage = (varTotalRecore % varPageSize == 0 ?
(varTotalRecore / varPageSize): Math.floor(varTotalRecore / varPageSize)+ 1);
this.jumpPage.minimum = 1;
this.jumpPage.maximum = _totalPage;
}
}
public function previousPage():void
{
this.curPage = this.curPage-1;
if ( this.curPage <= 1 )
{
this.curPage = 1;
this.preBtn.enabled = false;
this.nextBtn.enabled = true;
this.jumpPage.value = this.curPage;
this.startRecord = 0;
}
else
{
this.preBtn.enabled = true;
this.nextBtn.enabled = true;
this.jumpPage.value = this.curPage;
this.startRecord = (this.curPage-1)*pageSize;
}
this.getFunctionName(this._genFunction);
}
public function nextPage():void
{
this.curPage = this.curPage + 1;
if ( this.curPage>=_totalPage )
{
this.curPage =_totalPage;
this.nextBtn.enabled = false;
this.preBtn.enabled = true;
this.jumpPage.value = this.curPage;
this.startRecord = (this.curPage-1)*pageSize;
}
else
{
this.preBtn.enabled = true;
this.nextBtn.enabled = true;
this.jumpPage.value = this.curPage;
this.startRecord = (this.curPage-1)* pageSize;
}
this.getFunctionName(this._genFunction);
}
public function changePage():void
{
if ( this.curPage <= 1)
{
this.curPage = this.jumpPage.value;
this.preBtn.enabled = false;
this.nextBtn.enabled = true;
this.startRecord = 0;
}
else if ( this.curPage >= _totalPage )
{
this.curPage = this.jumpPage.value;
this.preBtn.enabled = true;
this.nextBtn.enabled = false;
this.startRecord = (this.curPage-1) * pageSize;
}
else
{
this.curPage = this.jumpPage.value;
this.preBtn.enabled = true;
this.nextBtn.enabled = true;
this.startRecord = (this.curPage-1)*pageSize ;
}
this.getFunctionName(this._genFunction);
}
public function changePageSize():void
{
this.curPage = 1;
this.startRecord = 0;
this.preBtn.enabled=false;
if(this.curPage < _totalPage)
{
this.nextBtn.enabled = true;
}
else
{
this.nextBtn.enabled = false;
}
this.jumpPage.value = this.curPage;
this.pageSize = this.recordPerPage.selectedItem.label;
computeThePageSize(totalRecord,this.pageSize);
this.getFunctionName(this._genFunction);
}
public function getFunctionName(functionName:Function):void
{
_genFunction(this.startRecord,this.pageSize);
}
]]>
</mx:Script>
<mx:Button id="preBtn" label="上一页" click="previousPage()"/>
<mx:Button id="nextBtn" label="下一页" click="nextPage()"/>
<mx:Label text="每页显示"/>
<mx:ComboBox id="recordPerPage" dataProvider="{acPageSize}" change="changePageSize()" height="25" width="64"/>
<mx:Label text="条"/>
<mx:Label text="跳转到第"/>
<mx:NumericStepper id="jumpPage" minimum="1" maximum="{_totalPage}" click="changePage()" />
<mx:Label text="页"/>
</mx:ControlBar>