嘿嘿,看到有人写这个对增删改查用面向对象进行包装 ,想下去年搞的项目里就是这样弄的,贴出来大家共享下吧,感觉没啥技术含量,就是思路还行吧。不废话了自己看吧,这事nhibernate自己搞的一个底层
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections;
5
using NHibernate;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace LogicInterface.lib
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
public class DBBaseDAL<T>
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
InsertOrUpdata#region InsertOrUpdata
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
13
/// 新增更新记录
14
/// </summary>
15
/// <param name="t">实体</param>
16
public static bool SaveOrUpdata(T t, SaveType type)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
ISession session = SessionManager.GetCurrentSession();
19
ITransaction tx = null;
20
bool isOk = false;
21
try
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
tx = session.BeginTransaction();
24
if (type == SaveType.Add)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
session.Save(t);
27
}
28
else
29
if (type == SaveType.Update)
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
session.Update(t);
32
}
33
else
34
if (type == SaveType.SaveOrUpdateCopy)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
session.SaveOrUpdateCopy(t);
37
session.Flush();
38
}
39
else
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
session.Save(t);
42
}
43
tx.Commit();
44
isOk = true;
45
}
46
catch (HibernateException ex)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
if (tx != null) tx.Rollback();
49
throw new CustomException("执行语句异常", ex.InnerException);
50
}
51
finally
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
SessionManager.CloseSession();
54
}
55
return isOk;
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
59
/// 批量更新
60
/// </summary>
61
/// <param name="objList"></param>
62
/// <returns></returns>
63
public static bool Update(List<T> objList)
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
bool isOk = false;
66
ISession session = SessionManager.GetCurrentSession();
67
session.BeginTransaction();
68
try
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
for (int i = 0; i < objList.Count; i++)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
session.Update(objList[i]);
73
}
74
session.Transaction.Commit();
75
isOk = true;
76
}
77
catch
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
session.Transaction.Rollback();
80
throw new CustomException(CustomExceptionType.ErrorExecHql, session);
81
}
82
finally
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
SessionManager.CloseSession();
85
}
86
return isOk;
87
}
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
#endregion
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Delete#region Delete
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
93
/// 删除记录
94
/// </summary>
95
/// <param name="t">实体</param>
96
public static bool Delete(T t)
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
98
ISession session = SessionManager.GetCurrentSession();
99
ITransaction tx = null;
100
bool isOk = false;
101
try
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
tx = session.BeginTransaction();
104
session.Delete(t);
105
tx.Commit();
106
isOk = true;
107
}
108
catch (HibernateException ex)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
if (tx != null) tx.Rollback();
111
throw new CustomException("执行语句异常", ex.InnerException);
112
}
113
finally
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
SessionManager.CloseSession();
116
}
117
return isOk;
118
}
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
120
/// 批量删除
121
/// </summary>
122
/// <param name="objs"></param>
123
/// <returns></returns>
124
public static bool Delete(IList<T> objs)
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126
bool isOk = false;
127
ISession session = SessionManager.GetCurrentSession();
128
session.BeginTransaction();
129
try
130![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
131
for (int i = 0; i < objs.Count; i++)
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
session.Delete(objs[i]);
134
}
135
session.Transaction.Commit();
136
isOk = true;
137
}
138
catch
139![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
140
session.Transaction.Rollback();
141
throw new CustomException(CustomExceptionType.ErrorExecHql, session);
142
}
143
finally
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
SessionManager.CloseSession();
146
}
147
return isOk;
148
}
149
#endregion
150![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
GetObject#region GetObject
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
153
/// 获取记录
154
/// </summary>
155
/// <param name="id">主键</param>
156
/// <returns>记录</returns>
157
public static T GetObject(object id)
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
ISession session = SessionManager.GetCurrentSession();
160
ITransaction tx = null;
161
try
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
tx = session.BeginTransaction();
164![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
165
T t = (T)session.Get<T>(id);
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167
tx.Commit();
168![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
169
return t;
170
}
171
catch (HibernateException ex)
172![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
173
if (tx != null) tx.Rollback();
174
throw new CustomException("执行语句异常", ex.InnerException);
175
}
176
finally
177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
SessionManager.CloseSession();
179
}
180
}
181
#endregion
182![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
183![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
GetObjects#region GetObjects
184![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
185
/// 获取全部记录
186
/// </summary>
187
/// <returns></returns>
188
public static IList<T> GetObjects()
189![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
190
return GetObjects(null, null);
191
}
192![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
193
/// 获取全部记录集合
194
/// </summary>
195
/// <param name="where">条件字段(不写where关键字)</param>
196
/// <returns>记录集合</returns>
197
public static IList<T> GetObjects(string where)
198![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
199
return GetObjects(where, null);
200
}
201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
202
/// 获取符合where记录集合
203
/// </summary>
204
/// <param name="where">条件字段(不写where关键字)</param>
205
/// <param name="orderBy">排序字段(不写orderby关键字)</param>
206
/// <returns>记录集合</returns>
207
public static IList<T> GetObjects(string where, string orderBy)
208![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
209
StringBuilder hql = new StringBuilder();
210
hql.AppendFormat("from {0} as tmp", typeof(T).Name);
211![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
212
if (!string.IsNullOrEmpty(where))
213![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
214
hql.AppendFormat(" where {0}", where);
215
}
216
if (!string.IsNullOrEmpty(orderBy))
217![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
218
hql.AppendFormat(" order by {0}", orderBy);
219
}
220
ISession session = SessionManager.GetCurrentSession();
221
ITransaction tx = null;
222
try
223![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
224
tx = session.BeginTransaction();
225
IList<T> list = session.CreateQuery(hql.ToString()).List<T>();
226
tx.Commit();
227
return list;
228
}
229
catch (HibernateException ex)
230![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
231
if (tx != null) tx.Rollback();
232
throw new CustomException("执行语句异常", ex.InnerException);
233
}
234
finally
235![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
236
SessionManager.CloseSession();
237
}
238
}
239![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
240![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
241
/// 分页GetObjects( T )/
242
/// <param name="where">条件(不带where关键字)</param>
243
/// <param name="orderBy">排序字段(不带orderBy关键字)</param>
244
/// <param name="pageIndex">当前页</param>
245
/// <param name="pageSize">页面大小</param>
246
/// <param name="recordCount">出参:总记录数</param>
247
/// <returns></returns>
248
public static IList<T> GetObjects(string where, string orderBy, int pageIndex, int pageSize, out int recordCount)
249![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
250
//拼Hql语句
251
StringBuilder hql = new StringBuilder();
252
hql.AppendFormat("from {0} as tmp", typeof(T).Name);
253
if (!string.IsNullOrEmpty(where))
254![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
255
hql.AppendFormat(" where {0}", where);
256
}
257
if (!string.IsNullOrEmpty(orderBy))
258![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
259
hql.AppendFormat(" order by {0}", orderBy);
260
}
261
ISession session = SessionManager.GetCurrentSession();
262
IQuery iquery = session.CreateQuery(hql.ToString());
263
iquery = pager(iquery, pageIndex, pageSize, out recordCount);//分页
264
IList<T> objs = iquery.List<T>();
265
SessionManager.CloseSession();
266
return objs;
267
}
268![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
269![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
270
/// 分页GetObjects( T )
271![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
272
/// </summary>
273
/// <param name="where"></param>
274
/// <param name="orderBy"></param>
275
/// <param name="pageIndex"></param>
276
/// <param name="pageSize"></param>
277
/// <param name="recordCount"></param>
278
/// <returns></returns>
279
public static IList<T> GetObjects_two(string hql, int pageIndex, int pageSize, out int recordCount)
280![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
281
//拼Hql语句
282![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
283![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
284
ISession session = SessionManager.GetCurrentSession();
285
try
286![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
287
DateTime da = DateTime.Now;
288![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
289
IQuery iquery = session.CreateQuery(hql).SetDateTime("curr_time", da);
290
iquery = pager(iquery, pageIndex, pageSize, out recordCount);//分页
291
IList<T> objs = iquery.List<T>();
292
return objs;
293![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
294
}
295
catch (Exception ex)
296![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
297
throw ex;
298![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
299
}
300
finally
301![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
302
SessionManager.CloseSession();
303
}
304
}
305![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
306
/// 传参数查询结果方法2
307
/// </summary>
308
/// <param name="where"></param>
309
/// <param name="orderBy"></param>
310
/// <param name="pageIndex"></param>
311
/// <param name="pageSize"></param>
312
/// <param name="recordCount"></param>
313
/// <returns></returns>
314
public static IList<T> GetObjectsByHql(string hql)
315![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
316
//拼Hql语句
317![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
318![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
319
ISession session = SessionManager.GetCurrentSession();
320
try
321![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
322
IQuery iq = session.CreateQuery(hql);
323
IList<T> list = iq.List<T>();
324
return list;
325![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
326![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
327
}
328
catch (Exception ex)
329![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
330
throw ex;
331![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
332
}
333
finally
334![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
335
SessionManager.CloseSession();
336
}
337
return null;
338![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
339![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
340
}
341![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
342
/// top 查询返回列表
343
/// </summary>
344
/// <param name="where"></param>
345
/// <param name="orderBy"></param>
346
/// <param name="topCount"></param>
347
/// <returns></returns>
348
public static IList<T> GetObjects(string where, string orderBy, int topCount)
349![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
350
//拼Hql语句
351
StringBuilder hql = new StringBuilder();
352
hql.AppendFormat("from {0} as tmp", typeof(T).Name);
353
if (!string.IsNullOrEmpty(where))
354![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
355
hql.AppendFormat(" where {0}", where);
356
}
357
if (!string.IsNullOrEmpty(orderBy))
358![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
359
hql.AppendFormat(" order by {0}", orderBy);
360
}
361
ISession session = SessionManager.GetCurrentSession();
362
IQuery iquery = session.CreateQuery(hql.ToString());
363
//实现top功能
364
iquery = iquery.SetFirstResult(0);
365
iquery = iquery.SetMaxResults(topCount);
366
IList<T> objs = iquery.List<T>();
367
SessionManager.CloseSession();
368
return objs;
369
}
370![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
371
/// 主要用于返回列表
372
/// </summary>
373
/// <param name="showRecodeNumber">显示多少条</param>
374
/// <param name="where">查询条件</param>
375
/// <param name="orderBy">排序字段</param>
376
/// <param name="sortStyle">排序方式</param>
377
/// <returns></returns>
378
public static IList<T> GetObjects(int showRecodeNumber, string where, string orderBy, string sortStyle)
379![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
380
//拼Hql语句
381
StringBuilder hql = new StringBuilder();
382
hql.AppendFormat("from {0} as tmp", typeof(T).Name);
383
if (!string.IsNullOrEmpty(where))
384![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
385
hql.AppendFormat(" where {0}", where);
386
}
387
if (!string.IsNullOrEmpty(orderBy))
388![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
389
if (StringTools.ObjectToStr(sortStyle).ToUpper().Trim() == "ASC")
390![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
391
hql.AppendFormat(" order by {0} {1}", orderBy, "ASC");
392
}
393
else
394![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
395
hql.AppendFormat(" order by {0} {1}", orderBy, "DESC");
396
}
397
}
398
ISession session = SessionManager.GetCurrentSession();
399
IQuery iquery = session.CreateQuery(hql.ToString());
400
//if (iquery.List<T>() != null && iquery.List<T>().Count >= showRecodeNumber)
401
//{
402
//实现top功能
403
iquery = iquery.SetFirstResult(0);
404
iquery = iquery.SetMaxResults(StringTools.ObjectToInt(showRecodeNumber));
405
IList<T> objs = iquery.List<T>();
406
SessionManager.CloseSession();
407
return objs;
408
//}
409
//return null;
410
}
411![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
412
//select top 5 company_name,Company_type,COMINATE_CATEGORY,COMINATE_DATETIME from mm_company
413
//where COMINATE_CATEGORY=100 and Company_type=1
414
//order by COMINATE_DATETIME desc
415![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
416
#endregion
417![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
418
}//class
419![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
420
}
421![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
422![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
嘿嘿写的不好,见谅!