不做浮躁的人
正在行走的人...
posts - 171,  comments - 51,  trackbacks - 0
延续上一篇博客,本文主要讲讲如果为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 不做浮躁的人 阅读(1480) 评论(0)  编辑  收藏 所属分类: air

只有注册用户登录后才能发表评论。


网站导航:
 

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

常用链接

留言簿(9)

随笔分类(31)

随笔档案(75)

文章分类(1)

文章档案(3)

搜索

  •  

最新评论

阅读排行榜

评论排行榜