Castle.ActiveRecord的分页示例:
分页结果泛型类:
Code
1
namespace EasyNet.Core
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
4
/// 分页结果
5
/// </summary>
6
/// <typeparam name="T"></typeparam>
7
public class PagedResult<T>
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// 记录总数
11
/// </summary>
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Total
{ get; set; }
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
/// 结果数据
16
/// </summary>
17![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public T Data
{ get; set; }
18
}
19
}
20
文章分类(典型树形结构):
Code
1
namespace EasyNet.Model
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
using System.Collections.Generic;
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
using NHibernate.Criterion;
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
using Castle.ActiveRecord;
8
9
[ActiveRecord("site_articlecategory")]
10
public class ArticleCategory : ActiveRecordBase<ArticleCategory>
11![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
[PrimaryKey(PrimaryKeyType.Native)]
13![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Id
{ get; set; }
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
[Property]
16![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
[Property]
19![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool ShowInDefault
{ get; set; }
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
[BelongsTo("ParentId")]
22![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ArticleCategory Parent
{ get; set; }
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
[HasMany(typeof(ArticleCategory), "ParentID", "site_articlecategory", Cascade = ManyRelationCascadeEnum.All)]
25![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public IList<ArticleCategory> Children
{ get; set; }
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
[Newtonsoft.Json.JsonIgnore]
28
[HasAndBelongsToMany(Table = "site_articleinarticlecategory", ColumnRef = "ArticleId", ColumnKey = "CategoryId", Lazy = true)]
29![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public IList<Article> Articles
{ get; set; }
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
}
32
}
33
文章实体(定义了一个根据文章分类分页查询,文章与文章分类是多对多的关系):
Code
1
namespace EasyNet.Model
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
using System.Collections.Generic;
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
using NHibernate.Criterion;
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
using Castle.ActiveRecord;
8
using Castle.ActiveRecord.Queries;
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
using Core;
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
[ActiveRecord("site_article")]
13
public class Article : ActiveRecordBase<Article>
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
[PrimaryKey(PrimaryKeyType.Native)]
16![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Id
{ get; set; }
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
[Property]
19![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Title
{ get; set; }
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
[Property]
22![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Content
{ get; set; }
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
[Property]
25![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Summary
{ get; set; }
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
[Property]
28![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Author
{ get; set; }
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
[Property]
31![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool IsNewsPhotos
{ get; set; }
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
[Property]
34![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string PhotoUrl
{ get; set; }
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
[BelongsTo("TemplateId")]
37![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ArticleTemplate Template
{ get; set; }
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
[BelongsTo("CreatorId")]
40![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public User Creator
{ get; set; }
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
[BelongsTo("ModifierId")]
43![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public User Modifier
{ get; set; }
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
[Property]
46![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool Published
{ get; set; }
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
[Property]
49![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool Deleted
{ get; set; }
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
[Property]
52![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public long CreatedDatetime
{ get; set; }
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
[Property]
55![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public long UpdatedDatetime
{ get; set; }
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
[Newtonsoft.Json.JsonIgnore]
58
[HasAndBelongsToMany(Table = "site_articleinarticlecategory", ColumnRef = "CategoryId", ColumnKey = "ArticleId", Lazy = true)]
59![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public IList<ArticleCategory> Categories
{ get; set; }
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
public static PagedResult<Article[]> FindByCategory(int categoryId, int start, int limit)
62![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
CountQuery countQuery = new CountQuery(typeof(Article));
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
countQuery.SetParameter("Id", categoryId);
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
countQuery.Query = "select count(*) from Article as article left join article.Categories as category where category.Id in(:Id)";
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
int total = (int)ExecuteQuery(countQuery);
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
if (total == 0)
72![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
return null;
74
}
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
SimpleQuery<Article> query = new SimpleQuery<Article>("select article from Article as article left join article.Categories as category where category.Id in(:Id)");
77![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
query.SetParameter("Id", categoryId);
79
query.SetQueryRange(start, limit);
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
Article[] articles = query.Execute();
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
PagedResult<Article[]> result = new PagedResult<Article[]>();
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
result.Data = articles;
86
result.Total = total;
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
return result;
89
}
90
}
91
}
92