使用PetaPoco ORM 框架分页查询


通过在派生的Repository中调用GetPagingEntities方法来获取分页数据,并返回由PagingDataSet<T>封装分页集合,例如:

Public PagingDataSet<Student> GetDataPage(int? stuid,int pageIndex, int pageSize )
{
  return GetPagingEntities(pageSize,pageIndex,CachingExpirationType.ObjectCollection,()=>{
   StringBulider cacheKey = new StringBulider(CacheSetting.GetListCacheKeyPrefix(CacheVersionType.AreaVersion,"stuid",stuid));
   
   if(stuid.HasValue&& stuid.Value>0)
   {
    cacheKey.AppendFormat("stuid-{0}:", stuid.Value);
    
    return stuid.ToString();
    },
    ()=>{
    
     var sql = PetaPoco.Sql.Builder;
     if(stuid.HasValue&& && stuid.Value>0)
      sql.Where("stuid =@0", stuid.Value);
      return sql;
    }
  });
}
View Code

         对于查询分为主流查询及非主流查询:

1)         例如:博客文章列表属于主流查询,博客文章排行属于非主流查询;

2)         主流查询最大返回记录数限制为PrimaryMaxRecords;

3)         非主流查询通常不需要查看太多数据,最大返回记录数限制为SecondaryMaxRecords;

3.         对于查询条件较多的情况可以定义Query类进行封装;

Public class SampleEntityQuery{

  public long? userid=null;
  public SortBySampleEntity sort=SortBySampleEntity.DataCreated;
  
  public enum SortBySampleEntity{
  DateCreate,
  HitTimes
  }
}

 4.         查询语句需要使用PetaPoco.Sql进行组装;

5.         分页查询结果使用只读集合类型PagingDataSet<T>进行封装,PagingDataSet<T>中含当前集合的PageIndex、PageSize与TotalRecords信息;

原文地址:https://www.cnblogs.com/wisdo/p/4366393.html