本文关于NHibernate的Demo和效率测试,希望对大家有用.
1.先去官网下载Nhibernate
2.放入到项目中并建立Helper类
private static ISession _Session = null; public static ISession Session
{
get
{
if (_Session == null)
{ Configuration cfg = new Configuration(); // _Session session factory from configuration object
_Session = cfg.Configure(CurrentLocation + "Nhibernate.config").BuildSessionFactory().OpenSession();
} return _Session;
}
}
写操作方法
更新
public static string SaveOrUpdate<T>(T item)
{
Helper.Session.Clear();
string msg = string.Empty;
try
{
using (Helper.Session.BeginTransaction())
{
Helper.Session.SaveOrUpdate(item);
Helper.Session.Transaction.Commit();
}
}
catch (Exception ex)
{
throw ex;
} return msg;
}
public static string Save<T>(T item)
{
Helper.Session.Clear();
string msg = string.Empty;
try
{
using (Helper.Session.BeginTransaction())
{
Helper.Session.Save(item);
Helper.Session.Transaction.Commit();
}
}
catch (Exception ex)
{
throw ex;
} return msg;
}
public static string Update<T>(T item)
{
Helper.Session.Clear();
string msg = string.Empty;
try
{
using (Helper.Session.BeginTransaction())
{
Helper.Session.Update(item);
Helper.Session.Transaction.Commit();
}
}
catch (Exception ex)
{
throw ex;
} return msg;
}
public static string Delete<T>(T item)
{
Helper.Session.Clear();
string msg = string.Empty;
try
{
using (Helper.Session.BeginTransaction())
{
Helper.Session.Delete(item);
Helper.Session.Transaction.Commit();
}
}
catch (Exception ex)
{
throw ex;
} return msg;
}
public static string Delete<T>(List<T> itemsToDelete)
{
Helper.Session.Clear();
string msg = string.Empty;
try
{
using (Helper.Session.BeginTransaction())
{
foreach (T item in itemsToDelete)
{
Helper.Session.Delete(item);
}
Helper.Session.Transaction.Commit();
}
}
catch (Exception ex)
{
throw ex;
} return msg;
}
Delete
public static IList<T> GetEntityList<T>(IList<ICriterion> whereCondition)
{
return GetEntityList<T>(whereCondition, null);
}
获取数据实体列表
public static IList<T> GetEntityList<T>(IList<ICriterion> whereCondition,IList<string> orderColumnList)
{
Helper.Session.Clear();
ICriteria criteria = Session.CreateCriteria(typeof(T)); if (whereCondition != null && whereCondition.Count > )
{
foreach (ICriterion cri in whereCondition)
{
criteria.Add(cri);
}
} if (orderColumnList != null && orderColumnList.Count > )
{
foreach (string orderColumn in orderColumnList)
{
criteria.AddOrder(Order.Asc(orderColumn));
}
} return criteria.List<T>();
}
获取数据实体列表
public static void ExecuteProcedure(string procedureName, List<ProcedureParameter> lstParameters)
{
Helper.Session.Clear();
try
{
var cmd = Session.Connection.CreateCommand(); cmd.CommandText = procedureName;
cmd.CommandType = CommandType.StoredProcedure;
foreach (var para in lstParameters)
{
var iPara = cmd.CreateParameter();
iPara.ParameterName = para.ParameterName;
iPara.Value = para.Value;
iPara.Direction = para.Direction;
iPara.DbType = para.DataType;
if (para.Size != )
{
iPara.Size = para.Size;
}
cmd.Parameters.Add(iPara);
}
cmd.ExecuteNonQuery(); foreach (var p in lstParameters)
{
if (p.Direction == ParameterDirection.Output)
{
p.Value = ((System.Data.Common.DbParameter)cmd.Parameters[p.ParameterName]).Value;
}
}
}
catch(Exception ex)
{
throw ex;
}
}
执行存储过程
3.建立单元测试项目
略
最后,我知道没有代码你们是不会来的,so,如下 :
https://github.com/wujianfei01/NHibernate-Demo/
Ps:请用VS2013及以后版本打开