延續
上一篇博客,本文主要講講如果為flexorm增加分頁查詢的功能,當然是基于Criteria,flexorm沒有提供查詢語言,criteria是目前的首選。
1、我修改Criteria類,傳入兩個參數:
private var _firstResult:int=-1;
private var _maxResult:int=-1;
public function setFirstResult(value:int):Criteria {
_firstResult=value;
return this;
}
public function setMaxResult(value:int):Criteria {
_maxResult=value;
return this;
}
public function get firstResult():int {
return _firstResult;
}
public function get maxResult():int {
return _maxResult;
}
值得注意的是,我的setXX方法,不同傳統的setXX方法,返回Criteria自身,也是為了寫代碼方便。
2、在SelectCommand方法增加兩個參數定義:
private var _firstResult:int=-1;
private var _maxResult:int=-1;
默認值為-1。
3、在SelectCommand的setCriteria(crit:Criteria)方法后面增加以下代碼,將Criteria的新增加的兩個參數值傳進來。
_firstResult=crit.firstResult;
_maxResult=crit.maxResult;
4、在SelectCommand的prepareStatement()的_statement.text=sql;語句前增加以下代碼:
if (_firstResult != -1 && _maxResult != -1) {
sql+=" limit " + _firstResult + "," + _maxResult;
}
該代碼利用sqlite的limit語法進行分頁。
5、以上基本的功能代碼實現完畢,下面在EntityManager實現分頁的接口代碼:
public function findPage(page:Page, c:Criteria):Page {
page=page.cleanDatas();
if (page.countTotal) {
var totalCount:int=fetchCriteriaCountResult(c);
page.totalCount=totalCount;
}
validPageInfo(page);
if (page.totalCount == 0) {
return page;
}
c.setFirstResult(page.startNo);
c.setMaxResult(page.pageSize);
page.datas=fetchCriteria(c);
return page;
}
private function validPageInfo(page:Page):void {
if (page.pageNo == 0 || page.totalCount == 0) {
page.pageNo=1;
} else if (page.totalPageCnt < page.pageNo) {
page.pageNo=page.totalPageCnt;
}
}
6、其它:可以在Criteria上增加一個PropertyFilter的功能:
public function addCriteriaFilter(propertyFilters:ArrayCollection):Criteria {
for each (var pf:PropertyFilter in propertyFilters) {
if (StringUtils.isEmpty(pf.matchType) || pf.matchType == PropertyFilter.MATCHTYPE_EQ) {
this.addEqualsCondition(pf.matchField, pf.matchValue);
} else if (pf.matchType == PropertyFilter.MATCHTYPE_LIKE) {
this.addLikeCondition(pf.matchField, '%' + (pf.matchValue as String) + '%');
} else if (pf.matchType == PropertyFilter.MATCHTYPE_LIKESTART) {
this.addLikeCondition(pf.matchField, (pf.matchValue as String) + '%');
} else if (pf.matchType == PropertyFilter.MATCHTYPE_LIKEEND) {
this.addLikeCondition(pf.matchField, '%' + (pf.matchValue as String));
} else if (pf.matchType == PropertyFilter.MATCHTYPE_LT) {
this.addLessThanCondition(pf.matchField, pf.matchValue as String);
} else if (pf.matchType == PropertyFilter.MATCHTYPE_GT) {
this.addGreaterThanCondition(pf.matchField, pf.matchValue as String);
} else {
throw new Error("傳入的過濾匹配類型參數不正確。");
}
}
return this;
}
7、測試代碼:
private function searchUsers(_page:Page, _propertyFilters:ArrayCollection=null):void {
var c:Criteria=entityManager.createCriteria(IaUser).addCriteriaFilter(_propertyFilters);
page=entityManager.findPage(_page, c);
}
總結,采用limit實現分頁后,大數據量達到10萬后,性能差異將是數量級的提高。
posted on 2010-12-13 09:52
不做浮躁的人 閱讀(1501)
評論(0) 編輯 收藏 所屬分類:
air