zoukankan      html  css  js  c++  java
  • Castle ActiveRecord关系映射

    1.一对多的关系

    例如产品与品牌的关系,一种品牌可以有多个产品,一种产品对应一种品牌:

    产品(Product):

     1namespace EasyNet.Model
     2{
     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_product")]
    13    public class Product : ActiveRecordBase<Product>
    14    {
    15        [PrimaryKey(PrimaryKeyType.Native)]
    16        public int Id getset; }
    17
    18        [Property]
    19        public string Name getset; }
    20
    21        [Property]
    22        public string ShortDescription getset; }
    23
    24        [Property]
    25        public string FullDescription getset; }
    26
    27        [Property]
    28        public int DisplayOrder getset; }
    29
    30        [Property]
    31        public long CreatedDatetime getset; }
    32
    33        [Property]
    34        public long UpdatedDatetime getset; }
    35
    36        [Property]
    37        public string PictureUrl getset; }
    38
    39        [BelongsTo("BrandId")]
    40        public ProductBrand Brand getset; }
    41
    42
    43        [HasAndBelongsToMany(Table = "site_productinproductcategory", ColumnRef = "CategoryId", ColumnKey = "ProductId")]
    44        public IList<ProductCategory> Categories getset; }
    45
    46        public static void DeleteAllByIds(string ids)
    47        {
    48            Product.DeleteAll(string.Format("Id in ({0})", ids));
    49        }

    50
    51        public static PagedResult<Product[]> FindByCategory(int categoryId, int start, int limit)
    52        {
    53            CountQuery countQuery = new CountQuery(typeof(Product));
    54
    55            countQuery.SetParameter("Id", categoryId);
    56
    57            countQuery.Query = "select count(*) from Product as product left join product.Categories as category where category.Id in(:Id)";
    58
    59            int total = (int)ExecuteQuery(countQuery);
    60
    61            if (total == 0)
    62            {
    63                return null;
    64            }

    65
    66            SimpleQuery<Product> query = new SimpleQuery<Product>("select product from Product as product left join product.Categories as category where category.Id in(:Id) order by product.DisplayOrder desc, product.UpdatedDatetime desc");
    67
    68            query.SetParameter("Id", categoryId);
    69            query.SetQueryRange(start, limit); 
    70
    71
    72            Product[] products = query.Execute();
    73
    74            PagedResult<Product[]> result = new PagedResult<Product[]>();
    75
    76            result.Data = products;
    77            result.Total = total;
    78
    79            return result;
    80        }

    81    }

    82
    83}

    84

    产品品牌(ProductBrand):

     1namespace EasyNet.Model
     2{
     3    using System.Collections.Generic;
     4
     5    using NHibernate.Criterion;
     6
     7    using Castle.ActiveRecord;
     8    using Castle.ActiveRecord.Queries;
     9
    10    [ActiveRecord("site_productbrand")]
    11    public class ProductBrand : ActiveRecordBase<ProductBrand>
    12    {
    13        [PrimaryKey(PrimaryKeyType.Native)]
    14        public int Id getset; }
    15
    16        [Property]
    17        public string Name getset; }
    18
    19        [Property]
    20        public string LogoUrl getset; }
    21
    22        [Property]
    23        public int DisplayOrder getset; }
    24
    25        [HasMany(ColumnKey = "BrandId")]
    26        public IList<Product> Products getset; }
    27    }

    28}

    29

    2.多对多关系

    例如用户与角色的关系,一个用户可以有多种角色,一种角色对应多个用户:

    用户(User):

      1namespace EasyNet.Model
      2{
      3    using System.Collections.Generic;
      4    using System.Text;
      5
      6    using NHibernate.Criterion;
      7
      8    using Castle.ActiveRecord;
      9    using Castle.ActiveRecord.Queries;
     10
     11    using Core;
     12
     13    [ActiveRecord("site_user")]
     14    public class User : ActiveRecordBase<User>
     15    {
     16        [PrimaryKey(PrimaryKeyType.Native)]
     17        public int Id getset; }
     18
     19        [Property()]
     20        public string Username getset; }
     21
     22        [Property()]
     23        public string Password getset; }
     24
     25        [Property()]
     26        public string Name getset; }
     27
     28        [Property()]
     29        public bool IsApproved getset; }
     30
     31        [Property()]
     32        public int Gender getset; }
     33
     34        [Property()]
     35        public string Email getset; }
     36
     37        [Property()]
     38        public string Telephone getset; }
     39
     40        [Property()]
     41        public string Mobile getset; }
     42
     43        [Property(Default = "0")]
     44        public int Birthday getset; }
     45
     46        [Property(Default = "0")]
     47        public int Age getset; }
     48
     49        [Property(Default = "")]
     50        public string Company getset; }
     51
     52        [Property(Default = "")]
     53        public string Address getset; }
     54
     55        [Property(Default = "")]
     56        public string Website getset; }
     57
     58        [Property()]
     59        public long CreatedDatetime getset; }
     60
     61        [Property()]
     62        public long UpdatedDatetime getset; }
     63
     64        [HasAndBelongsToMany(Table = "site_userinrole", ColumnRef = "RoleId", ColumnKey = "UserId")]
     65        public IList<Role> Roles getset; }
     66
     67        /// <summary>
     68        /// 
     69        /// </summary>
     70        /// <param name="username"></param>
     71        /// <returns></returns>

     72        public static User FindByUsername(string username)
     73        {
     74            return FindOne(Expression.Eq("Username", username));
     75        }

     76
     77        public static PagedResult<User[]> FindAll(int type, string condition, int start, int limit)
     78        {
     79            CountQuery countQuery = new CountQuery(typeof(User));
     80
     81            StringBuilder sbCountQuery = new StringBuilder();
     82            StringBuilder sbQuery = new StringBuilder();
     83
     84            sbCountQuery.Append("select count(*) from User as user");
     85            sbQuery.Append("from User as user");
     86
     87            if (type == 0)
     88            {
     89                sbCountQuery.AppendFormat(" where user.Name like '%{0}%' or user.Email like '%{0}%'", condition);
     90                sbQuery.AppendFormat(" where user.Name like '%{0}%' or user.Email like '%{0}%'", condition);
     91
     92            }

     93            else if (type == 1)
     94            {
     95                sbCountQuery.AppendFormat(" where user.Name like '%{0}%'", condition);
     96                sbQuery.AppendFormat(" where user.Name like '%{0}%'", condition);
     97            }

     98            else if (type == 2)
     99            {
    100                sbCountQuery.AppendFormat(" where user.Email like '%{0}%'", condition);
    101                sbQuery.AppendFormat(" where user.Email like '%{0}%'", condition);
    102            }

    103
    104            countQuery.Query = sbCountQuery.ToString(); ;
    105
    106            int total = (int)ExecuteQuery(countQuery);
    107
    108            if (total == 0)
    109            {
    110                return null;
    111            }

    112
    113            SimpleQuery<User> query = new SimpleQuery<User>(sbQuery.ToString());
    114
    115            User[] users = query.Execute();
    116
    117            PagedResult<User[]> result = new PagedResult<User[]>();
    118
    119            result.Data = users;
    120            result.Total = total;
    121
    122            return result;
    123        }

    124    }

    125}

    126

    角色(Role):

     1namespace EasyNet.Model
     2{
     3    using System.Collections.Generic;
     4
     5    using Castle.ActiveRecord;
     6
     7    [ActiveRecord("site_role")]
     8    public class Role : ActiveRecordBase<Role>
     9    {
    10        [PrimaryKey(PrimaryKeyType.Native)]
    11        public int Id getset; }
    12
    13        [Property()]
    14        public string Name getset; }
    15
    17        [HasAndBelongsToMany(Table="site_userinrole", ColumnRef="UserId", ColumnKey="RoleId")]
    18        public IList<User> Users getset; }
    19    }

    20}

    21
  • 相关阅读:
    vue列表排序实现中的this问题
    JavaScript:JSON 和 JS 对象
    vue项目设置每个页面的title
    webpack开发和生产两个环境的配置详解
    关于vuex的理解
    vue的路由配置
    js 的静态获取和动态获取
    7 Dockerfile指令详解 && VOLUME 指令
    HAProxy负载均衡保持客户端和服务器Session亲缘性的3种方式
    haproxy开启日志功能
  • 原文地址:https://www.cnblogs.com/TerryLiang/p/1430114.html
Copyright © 2011-2022 走看看