前言:數據庫
項目爲傳統的三層架構,能夠根據我的的需求進行拓展.session
不少人都在質疑B層的做用,我認爲B層纔是核心,這個取決於業務的複雜度架構
項目的結構也比較的簡單,咱們先從最底層提及,ManagerPage,這是我定義的一個基類,它總共作了三件事,1.初始化NHibernate,2.解析參數模板,3.建立ICriteria(條件查詢器對象)返回查詢結果ui
1.初始化NHibernatespa
1 /// <summary> 2 /// 連接信息,初始化NH 3 /// </summary> 4 public static ISessionFactory SessionFactory 5 { 6 get 7 { 8 if (_sessionFactory == null) 9 { 10 var path = HttpContext.Current.Server.MapPath("/bin/hibernate.cfg.xml"); 11 var cfg = new NHibernate.Cfg.Configuration().Configure(path); 12 _sessionFactory = cfg.BuildSessionFactory(); 13 } 14 return _sessionFactory; 15 } 16 }
注意,sessionFactory是建立session的工廠,一般一個數據庫建立一個便可,是比較耗資源的一個地方,因此必須用到單例hibernate
2.解析參數模板code
1 public class SearchTemplate 2 { 3 public string key { get; set; } 4 public object value { get; set; } 5 public Common.EnumBase.SearchType searchType { get; set; } 6 7 }
參數模板有三個屬性,分別是 key對應表裏的字段,value對應值,searchType對應操做類型,這裏是一個枚舉xml
1 public enum SearchType 2 { 3 /// <summary> 4 /// 等於 5 /// </summary> 6 Eq = 1, 7 /// <summary> 8 /// 大於 9 /// </summary> 10 Gt =2, 11 /// <summary> 12 /// 大於等於 13 /// </summary> 14 Ge=3, 15 /// <summary> 16 /// 小於 17 /// </summary> 18 Lt=4, 19 /// <summary> 20 /// 小於等於 21 /// </summary> 22 Le =5, 23 /// <summary> 24 /// 等於空值 25 /// </summary> 26 IsNull =6, 27 /// <summary> 28 /// 非空值 29 /// </summary> 30 IsNotNull=7, 31 /// <summary> 32 /// 模糊查詢 xx% 33 /// </summary> 34 Like=8, 35 /// <summary> 36 /// 模糊查詢 %xx 37 /// </summary> 38 StartLike = 9, 39 /// <summary> 40 /// 等於列表中的某一個值 41 /// </summary> 42 In =10, 43 /// <summary> 44 /// 不等於列表中任意一個值 45 /// </summary> 46 NotIn=11, 47 /// <summary> 48 /// 分頁{pageindex,pagesize} 49 /// </summary> 50 Paging = 12, 51 }
這裏能夠根據本身的需求在這裏定義,這裏定義的都是ICriteria支持的操做對象
3.建立ICriteria對象blog
1 private static ICriteria GetCrit(List<SearchTemplate> list, ICriteria crit,int type = 1) 2 { 3 foreach (var item in list) 4 { 5 if (item.value == null) continue; 6 if (item.value.GetType() == typeof(String)) 7 { 8 if (item.value.ToString() == "") continue; 9 } 10 if (item.searchType.ToString() == Common.EnumBase.SearchType.Eq.ToString()) 11 { 12 crit.Add(Restrictions.Eq(item.key, item.value)); 13 continue; 14 } 15 if (item.searchType.ToString() == Common.EnumBase.SearchType.Gt.ToString()) 16 { 17 crit.Add(Restrictions.Gt(item.key, item.value)); 18 continue; 19 } 20 if (item.searchType.ToString() == Common.EnumBase.SearchType.Ge.ToString()) 21 { 22 crit.Add(Restrictions.Ge(item.key, item.value)); 23 continue; 24 } 25 if (item.searchType.ToString() == Common.EnumBase.SearchType.Lt.ToString()) 26 { 27 crit.Add(Restrictions.Lt(item.key, item.value)); 28 continue; 29 } 30 if (item.searchType.ToString() == Common.EnumBase.SearchType.Le.ToString()) 31 { 32 crit.Add(Restrictions.Le(item.key, item.value)); 33 continue; 34 } 35 if (item.searchType.ToString() == Common.EnumBase.SearchType.IsNull.ToString()) 36 { 37 crit.Add(Restrictions.IsNull(item.key)); 38 continue; 39 } 40 if (item.searchType.ToString() == Common.EnumBase.SearchType.IsNotNull.ToString()) 41 { 42 crit.Add(Restrictions.IsNotNull(item.key)); 43 continue; 44 } 45 if (item.searchType.ToString() == Common.EnumBase.SearchType.Like.ToString()) 46 { 47 crit.Add(Restrictions.Like(item.key, item.value + "%")); 48 continue; 49 } 50 if (item.searchType.ToString() == Common.EnumBase.SearchType.StartLike.ToString()) 51 { 52 crit.Add(Restrictions.Like(item.key, "%" + item.value)); 53 continue; 54 } 55 if (item.searchType.ToString() == Common.EnumBase.SearchType.In.ToString()) 56 { 57 crit.Add(Restrictions.In(item.key, (object[])item.value)); 58 continue; 59 } 60 if (item.searchType.ToString() == Common.EnumBase.SearchType.NotIn.ToString()) 61 { 62 crit.Add(Restrictions.Not(Restrictions.In(item.key, (object[])item.value))); 63 continue; 64 } 65 if (item.searchType.ToString() == Common.EnumBase.SearchType.Paging.ToString() && type == 1) 66 { 67 int[] paging = (int[])item.value; 68 crit.SetFirstResult((paging[0] - 1) * paging[1]); 69 crit.SetMaxResults(paging[1]); 70 continue; 71 } 72 } 73 return crit; 74 }
這裏返回ICriteria對象,NHibernate的查詢方式有多種,你能夠根據你的喜愛進行拓展Query Over,HQL,ICriteria,Linq