欢迎来到提伯斯的园子
|

提伯斯

园龄:9年9个月粉丝:138关注:7

LSJ_NHibernate第二章 ManagerPage

前言:

  项目为传统的三层架构,可以根据个人的需求进行拓展.

很多人都在质疑B层的作用,我认为B层才是核心,这个取决于业务的复杂度

                                                            

项目的结构也比较的简单,我们先从最底层说起,ManagerPage,这是我定义的一个基类,它总共做了三件事,1.初始化NHibernate,2.解析参数模板,3.创建ICriteria(条件查询器对象)返回查询结果

1.初始化NHibernate

复制代码
 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的工厂,通常一个数据库创建一个即可,是比较耗资源的一个地方,所以必须用到单例

2.解析参数模板

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对应操作类型,这里是一个枚举

复制代码
 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对象

复制代码
 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

 

本文作者:提伯斯

本文链接:https://www.cnblogs.com/tibos/p/6202114.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   提伯斯  阅读(470)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起