不做浮躁的人
          正在行走的人...
          posts - 171,  comments - 51,  trackbacks - 0
          延續(xù)上一篇博客,本文主要講講如果為flexorm增加分頁(yè)查詢的功能,當(dāng)然是基于Criteria,flexorm沒(méi)有提供查詢語(yǔ)言,criteria是目前的首選。

          1、我修改Criteria類,傳入兩個(gè)參數(shù):

          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方法,不同傳統(tǒng)的setXX方法,返回Criteria自身,也是為了寫代碼方便。

          2、在SelectCommand方法增加兩個(gè)參數(shù)定義:
            private var _firstResult:int=-1;
            private var _maxResult:int=-1;
          默認(rèn)值為-1。

          3、在SelectCommand的setCriteria(crit:Criteria)方法后面增加以下代碼,將Criteria的新增加的兩個(gè)參數(shù)值傳進(jìn)來(lái)。
          _firstResult=crit.firstResult;
             _maxResult=crit.maxResult;

          4、在SelectCommand的prepareStatement()的_statement.text=sql;語(yǔ)句前增加以下代碼:

          if (_firstResult != -1 && _maxResult != -1) {
              sql+=" limit " + _firstResult + "," + _maxResult;
             }

          該代碼利用sqlite的limit語(yǔ)法進(jìn)行分頁(yè)。

          5、以上基本的功能代碼實(shí)現(xiàn)完畢,下面在EntityManager實(shí)現(xiàn)分頁(yè)的接口代碼:

          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上增加一個(gè)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("傳入的過(guò)濾匹配類型參數(shù)不正確。");
              }
             }
             return this;
            }
          7、測(cè)試代碼:
          private function searchUsers(_page:Page, _propertyFilters:ArrayCollection=null):void {
              var c:Criteria=entityManager.createCriteria(IaUser).addCriteriaFilter(_propertyFilters);
              page=entityManager.findPage(_page, c);
             }
          總結(jié),采用limit實(shí)現(xiàn)分頁(yè)后,大數(shù)據(jù)量達(dá)到10萬(wàn)后,性能差異將是數(shù)量級(jí)的提高。
          posted on 2010-12-13 09:52 不做浮躁的人 閱讀(1502) 評(píng)論(0)  編輯  收藏 所屬分類: air

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          <2010年12月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(9)

          隨筆分類(31)

          隨筆檔案(75)

          文章分類(1)

          文章檔案(3)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 合山市| 宝鸡市| 绵竹市| 通辽市| 共和县| 红原县| 新营市| 平罗县| 汤原县| 马山县| 同江市| 靖远县| 比如县| 富民县| 马鞍山市| 阳高县| 阿克陶县| 沙雅县| 当阳市| 伊宁县| 偃师市| 三都| 蚌埠市| 嘉峪关市| 杨浦区| 柳州市| 蒲城县| 普洱| 霍林郭勒市| 郁南县| 福海县| 武乡县| 铁岭市| 喀喇沁旗| 沂水县| 郁南县| 卢氏县| 建德市| 辽源市| 广西| 浠水县|