本文关于NHibernate的Demo和效率测试,希望对大家有用.
1.先去官网下载Nhibernate
2.放入到项目中并建立Helper类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private static ISession _Session = null; 2 3 public static ISession Session 4 { 5 get 6 { 7 if (_Session == null) 8 { 9 10 Configuration cfg = new Configuration(); 11 12 // _Session session factory from configuration object 13 _Session = cfg.Configure(CurrentLocation + "Nhibernate.config").BuildSessionFactory().OpenSession(); 14 } 15 16 return _Session; 17 } 18 }
写操作方法
更新
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static string SaveOrUpdate<T>(T item) 2 { 3 Helper.Session.Clear(); 4 string msg = string.Empty; 5 try 6 { 7 using (Helper.Session.BeginTransaction()) 8 { 9 Helper.Session.SaveOrUpdate(item); 10 Helper.Session.Transaction.Commit(); 11 } 12 } 13 catch (Exception ex) 14 { 15 throw ex; 16 } 17 18 return msg; 19 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static string Save<T>(T item) 2 { 3 Helper.Session.Clear(); 4 string msg = string.Empty; 5 try 6 { 7 using (Helper.Session.BeginTransaction()) 8 { 9 Helper.Session.Save(item); 10 Helper.Session.Transaction.Commit(); 11 } 12 } 13 catch (Exception ex) 14 { 15 throw ex; 16 } 17 18 return msg; 19 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static string Update<T>(T item) 2 { 3 Helper.Session.Clear(); 4 string msg = string.Empty; 5 try 6 { 7 using (Helper.Session.BeginTransaction()) 8 { 9 Helper.Session.Update(item); 10 Helper.Session.Transaction.Commit(); 11 } 12 } 13 catch (Exception ex) 14 { 15 throw ex; 16 } 17 18 return msg; 19 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static string Delete<T>(T item) 2 { 3 Helper.Session.Clear(); 4 string msg = string.Empty; 5 try 6 { 7 using (Helper.Session.BeginTransaction()) 8 { 9 Helper.Session.Delete(item); 10 Helper.Session.Transaction.Commit(); 11 } 12 } 13 catch (Exception ex) 14 { 15 throw ex; 16 } 17 18 return msg; 19 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static string Delete<T>(List<T> itemsToDelete) 2 { 3 Helper.Session.Clear(); 4 string msg = string.Empty; 5 try 6 { 7 using (Helper.Session.BeginTransaction()) 8 { 9 foreach (T item in itemsToDelete) 10 { 11 Helper.Session.Delete(item); 12 } 13 Helper.Session.Transaction.Commit(); 14 } 15 } 16 catch (Exception ex) 17 { 18 throw ex; 19 } 20 21 return msg; 22 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static IList<T> GetEntityList<T>(IList<ICriterion> whereCondition) 2 { 3 return GetEntityList<T>(whereCondition, null); 4 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static IList<T> GetEntityList<T>(IList<ICriterion> whereCondition,IList<string> orderColumnList) 2 { 3 Helper.Session.Clear(); 4 ICriteria criteria = Session.CreateCriteria(typeof(T)); 5 6 if (whereCondition != null && whereCondition.Count > 0) 7 { 8 foreach (ICriterion cri in whereCondition) 9 { 10 criteria.Add(cri); 11 } 12 } 13 14 if (orderColumnList != null && orderColumnList.Count > 0) 15 { 16 foreach (string orderColumn in orderColumnList) 17 { 18 criteria.AddOrder(Order.Asc(orderColumn)); 19 } 20 } 21 22 return criteria.List<T>(); 23 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void ExecuteProcedure(string procedureName, List<ProcedureParameter> lstParameters) 2 { 3 Helper.Session.Clear(); 4 try 5 { 6 var cmd = Session.Connection.CreateCommand(); 7 8 cmd.CommandText = procedureName; 9 cmd.CommandType = CommandType.StoredProcedure; 10 foreach (var para in lstParameters) 11 { 12 var iPara = cmd.CreateParameter(); 13 iPara.ParameterName = para.ParameterName; 14 iPara.Value = para.Value; 15 iPara.Direction = para.Direction; 16 iPara.DbType = para.DataType; 17 if (para.Size != 0) 18 { 19 iPara.Size = para.Size; 20 } 21 cmd.Parameters.Add(iPara); 22 } 23 cmd.ExecuteNonQuery(); 24 25 foreach (var p in lstParameters) 26 { 27 if (p.Direction == ParameterDirection.Output) 28 { 29 p.Value = ((System.Data.Common.DbParameter)cmd.Parameters[p.ParameterName]).Value; 30 } 31 } 32 } 33 catch(Exception ex) 34 { 35 throw ex; 36 } 37 }
3.建立单元测试项目
略
最后,我知道没有代码你们是不会来的,so,如下 :
https://github.com/wujianfei01/NHibernate-Demo/
Ps:请用VS2013及以后版本打开