当前文件路径:AspxNuke/Common/NHibernate/BaseDAL.cs 
1
using System.Collections.Generic;
2
using NHibernate;
3
using System.Text;
4
5
namespace AspxNuke.NH
6
...{
7
/**//// <summary>
8
/// 数据层基类
9
/// </summary>
10
/// <typeparam name="T">实体类型</typeparam>
11
public class BaseDAL<T> : IBaseDAL<T>
12
...{
13
/**//// <summary>
14
/// 新增记录
15
/// </summary>
16
/// <param name="t">实体</param>
17
public virtual void Insert(T t)
18
...{
19
ISession session = NHibernateHelper.GetCurrentSession();
20
ITransaction tx = null;
21
try
22
...{
23
tx = session.BeginTransaction();
24
25
session.Save(t);
26
_getIdentity = (int)session.GetIdentifier(t);
27
session.Flush();
28
29
tx.Commit();
30
}
31
catch (HibernateException ex)
32
...{
33
if (tx != null) tx.Rollback();
34
throw ex;
35
}
36
finally
37
...{
38
NHibernateHelper.CloseSession();
39
}
40
}
41
42
/**//// <summary>
43
/// 更新记录
44
/// </summary>
45
/// <param name="t">实体</param>
46
public virtual void Update(T t)
47
...{
48
ISession session = NHibernateHelper.GetCurrentSession();
49
ITransaction tx = null;
50
try
51
...{
52
tx = session.BeginTransaction();
53
54
session.Update(t);
55
session.Flush();
56
57
tx.Commit();
58
}
59
catch (HibernateException ex)
60
...{
61
if (tx != null) tx.Rollback();
62
throw ex;
63
}
64
finally
65
...{
66
NHibernateHelper.CloseSession();
67
}
68
}
69
70
/**//// <summary>
71
/// 删除记录
72
/// </summary>
73
/// <param name="t">实体</param>
74
public virtual void Delete(T t)
75
...{
76
ISession session = NHibernateHelper.GetCurrentSession();
77
ITransaction tx = null;
78
try
79
...{
80
tx = session.BeginTransaction();
81
82
session.Delete(t);
83
session.Flush();
84
85
tx.Commit();
86
}
87
catch (HibernateException ex)
88
...{
89
if (tx != null) tx.Rollback();
90
throw ex;
91
}
92
finally
93
...{
94
NHibernateHelper.CloseSession();
95
}
96
}
97
98
/**//// <summary>
99
/// 获取记录
100
/// </summary>
101
/// <param name="id">主键</param>
102
/// <returns>记录</returns>
103
public virtual T GetObject(int id)
104
...{
105
ISession session = NHibernateHelper.GetCurrentSession();
106
ITransaction tx = null;
107
try
108
...{
109
tx = session.BeginTransaction();
110
111
T t = (T)session.Get(typeof(T), id);
112
113
tx.Commit();
114
115
return t;
116
}
117
catch (HibernateException ex)
118
...{
119
if (tx != null) tx.Rollback();
120
throw ex;
121
}
122
finally
123
...{
124
NHibernateHelper.CloseSession();
125
}
126
}
127
获取记录集合#region 获取记录集合
128
/**//// <summary>
129
/// 获取记录集合
130
/// </summary>
131
/// <returns>记录集合</returns>
132
public virtual IList<T> GetObjects()
133
...{
134
string hql = string.Format("From {0} as tmp", typeof(T).Name);
135
return GetObjects(hql);
136
}
137
138
/**//// <summary>
139
/// 获取记录集合
140
/// </summary>
141
/// <param name="hql">查询语句</param>
142
/// <returns>记录集合</returns>
143
public virtual IList<T> GetObjects(string hql)
144
...{
145
ISession session = NHibernateHelper.GetCurrentSession();
146
ITransaction tx = null;
147
try
148
...{
149
tx = session.BeginTransaction();
150
151
IList<T> list = session.CreateQuery(hql).List<T>();
152
153
tx.Commit();
154
155
return list;
156
}
157
catch (HibernateException ex)
158
...{
159
if (tx != null) tx.Rollback();
160
throw ex;
161
}
162
finally
163
...{
164
NHibernateHelper.CloseSession();
165
}
166
}
167
168
/**//// <summary>
169
/// 获取记录集合
170
/// </summary>
171
/// <param name="start">设置首个对象返回的位置</param>
172
/// <param name="max">设置返回的最大结果数</param>
173
/// <returns>记录集合</returns>
174
public virtual IList<T> GetObjects(int start, int max)
175
...{
176
string hql = string.Format("From {0} as tmp", typeof(T).Name);
177
return GetObjects(hql, start, max);
178
}
179
180
/**//// <summary>
181
/// 获取记录集合
182
/// </summary>
183
/// <param name="hql">查询语句</param>
184
/// <param name="start">设置首个对象返回的位置</param>
185
/// <param name="max">设置返回的最大结果数</param>
186
/// <returns>记录集合</returns>
187
public virtual IList<T> GetObjects(string hql, int start, int max)
188
...{
189
ISession session = NHibernateHelper.GetCurrentSession();
190
ITransaction tx = null;
191
try
192
...{
193
tx = session.BeginTransaction();
194
195
_getRowCount = session.CreateQuery(hql).List().Count;
196
197
IList<T> list = session.CreateQuery(hql).SetFirstResult(start).SetMaxResults(max).List<T>();
198
tx.Commit();
199
200
return list;
201
}
202
catch (HibernateException ex)
203
...{
204
if (tx != null) tx.Rollback();
205
throw ex;
206
}
207
finally
208
...{
209
NHibernateHelper.CloseSession();
210
}
211
}
212
213
/**//// <summary>
214
/// 获取记录集合
215
/// </summary>
216
/// <param name="where">查询条件</param>
217
/// <param name="orderBy">排序</param>
218
/// <returns>记录集合</returns>
219
public virtual IList<T> GetObjects(string where, string orderBy)
220
...{
221
StringBuilder hql = new StringBuilder();
222
hql.AppendFormat("from {0} as tmp", typeof(T).Name);
223
if (!string.IsNullOrEmpty(where))
224
...{
225
hql.AppendFormat(" where {0}", where);
226
}
227
if (!string.IsNullOrEmpty(orderBy))
228
...{
229
hql.AppendFormat(" order by {0}", orderBy);
230
}
231
232
return GetObjects(hql.ToString());
233
}
234
235
/**//// <summary>
236
/// 获取记录集合
237
/// </summary>
238
/// <param name="where">查询条件</param>
239
/// <param name="orderBy">排序</param>
240
/// <param name="pageNo">当前页码</param>
241
/// <param name="pageSize">页长</param>
242
/// <returns>记录集合</returns>
243
public virtual IList<T> GetObjects(string where, string orderBy, int pageNo, int pageSize)
244
...{
245
int start = (pageNo - 1) * pageSize;
246
int max = pageSize;
247
StringBuilder hql = new StringBuilder();
248
hql.AppendFormat("from {0} as tmp", typeof(T).Name);
249
if (!string.IsNullOrEmpty(where))
250
...{
251
hql.AppendFormat(" where {0}", where);
252
}
253
if (!string.IsNullOrEmpty(orderBy))
254
...{
255
hql.AppendFormat(" order by {0}", orderBy);
256
}
257
258
return GetObjects(hql.ToString(), start, max);
259
}
260
#endregion
261
262
private int _getRowCount;
263
/**//// <summary>
264
/// 获取记录行数,只有分页时有数值,其它为0
265
/// </summary>
266
public int GetRowCount
267
...{
268
get ...{ return _getRowCount; }
269
}
270
271
private int _getIdentity;
272
/**//// <summary>
273
/// 获取主键
274
/// </summary>
275
public int GetIdentity
276
...{
277
get ...{ return _getIdentity; }
278
}
279
}
280
}
281