Castle.ActiveRecord分页示例

Castle.ActiveRecord的分页示例:

分页结果泛型类:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1namespace EasyNet.Core
 2ExpandedBlockStart.gifContractedBlock.gif{
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 4    /// 分页结果
 5    /// </summary>
 6    /// <typeparam name="T"></typeparam>

 7    public class PagedResult<T>
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
10        /// 记录总数
11        /// </summary>

12ExpandedSubBlockStart.gifContractedSubBlock.gif        public int Total getset; }
13
14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
15        /// 结果数据
16        /// </summary>

17ExpandedSubBlockStart.gifContractedSubBlock.gif        public T Data getset; }
18    }

19}

20

 

文章分类(典型树形结构):

ContractedBlock.gif ExpandedBlockStart.gif Code
 1namespace EasyNet.Model
 2ExpandedBlockStart.gifContractedBlock.gif{
 3    using System.Collections.Generic;
 4
 5    using NHibernate.Criterion;
 6
 7    using Castle.ActiveRecord;
 8    
 9    [ActiveRecord("site_articlecategory")]
10    public class ArticleCategory : ActiveRecordBase<ArticleCategory>
11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
12        [PrimaryKey(PrimaryKeyType.Native)]
13ExpandedSubBlockStart.gifContractedSubBlock.gif        public int Id getset; }
14
15        [Property]
16ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Name getset; }
17
18        [Property]
19ExpandedSubBlockStart.gifContractedSubBlock.gif        public bool ShowInDefault getset; }
20
21        [BelongsTo("ParentId")]
22ExpandedSubBlockStart.gifContractedSubBlock.gif        public ArticleCategory Parent getset; }
23
24        [HasMany(typeof(ArticleCategory), "ParentID""site_articlecategory", Cascade = ManyRelationCascadeEnum.All)]
25ExpandedSubBlockStart.gifContractedSubBlock.gif        public IList<ArticleCategory> Children getset; }
26
27        [Newtonsoft.Json.JsonIgnore]
28        [HasAndBelongsToMany(Table = "site_articleinarticlecategory", ColumnRef = "ArticleId", ColumnKey = "CategoryId", Lazy = true)]
29ExpandedSubBlockStart.gifContractedSubBlock.gif        public IList<Article> Articles getset; }
30
31    }

32}

33

文章实体(定义了一个根据文章分类分页查询,文章与文章分类是多对多的关系):


ContractedBlock.gif ExpandedBlockStart.gif Code
 1namespace EasyNet.Model
 2ExpandedBlockStart.gifContractedBlock.gif{
 3    using System.Collections.Generic;
 4
 5    using NHibernate.Criterion;
 6
 7    using Castle.ActiveRecord;
 8    using Castle.ActiveRecord.Queries;
 9
10    using Core;
11
12    [ActiveRecord("site_article")]
13    public class Article : ActiveRecordBase<Article>
14ExpandedSubBlockStart.gifContractedSubBlock.gif    {
15        [PrimaryKey(PrimaryKeyType.Native)]
16ExpandedSubBlockStart.gifContractedSubBlock.gif        public int Id getset; }
17
18        [Property]
19ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Title getset; }
20
21        [Property]
22ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Content getset; }
23
24        [Property]
25ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Summary getset; }
26
27        [Property]
28ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Author getset; }
29
30        [Property]
31ExpandedSubBlockStart.gifContractedSubBlock.gif        public bool IsNewsPhotos getset; }
32
33        [Property]
34ExpandedSubBlockStart.gifContractedSubBlock.gif        public string PhotoUrl getset; }
35
36        [BelongsTo("TemplateId")]
37ExpandedSubBlockStart.gifContractedSubBlock.gif        public ArticleTemplate Template getset; }
38
39        [BelongsTo("CreatorId")]
40ExpandedSubBlockStart.gifContractedSubBlock.gif        public User Creator getset; }
41
42        [BelongsTo("ModifierId")]
43ExpandedSubBlockStart.gifContractedSubBlock.gif        public User Modifier getset; }
44
45        [Property]
46ExpandedSubBlockStart.gifContractedSubBlock.gif        public bool Published getset; }
47
48        [Property]
49ExpandedSubBlockStart.gifContractedSubBlock.gif        public bool Deleted getset; }
50
51        [Property]
52ExpandedSubBlockStart.gifContractedSubBlock.gif        public long CreatedDatetime getset; }
53
54        [Property]
55ExpandedSubBlockStart.gifContractedSubBlock.gif        public long UpdatedDatetime getset; }
56
57        [Newtonsoft.Json.JsonIgnore]
58        [HasAndBelongsToMany(Table = "site_articleinarticlecategory", ColumnRef = "CategoryId", ColumnKey = "ArticleId", Lazy = true)]
59ExpandedSubBlockStart.gifContractedSubBlock.gif        public IList<ArticleCategory> Categories getset; }
60
61        public static PagedResult<Article[]> FindByCategory(int categoryId, int start, int limit)
62ExpandedSubBlockStart.gifContractedSubBlock.gif        {
63            CountQuery countQuery = new CountQuery(typeof(Article));
64
65            countQuery.SetParameter("Id", categoryId);
66
67            countQuery.Query = "select count(*) from Article as article left join article.Categories as category where category.Id in(:Id)";
68
69            int total = (int)ExecuteQuery(countQuery);
70
71            if (total == 0)
72ExpandedSubBlockStart.gifContractedSubBlock.gif            {
73                return null;
74            }

75
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
78            query.SetParameter("Id", categoryId);
79            query.SetQueryRange(start, limit);
80
81            Article[] articles = query.Execute();
82
83            PagedResult<Article[]> result = new PagedResult<Article[]>();
84
85            result.Data = articles;
86            result.Total = total;
87
88            return result;
89        }

90    }

91}

92

转载于:https://www.cnblogs.com/TerryLiang/archive/2009/03/18/1415235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值