NHibernate的session工厂
using System; using System.Web; using NHibernate; using NHibernate.Cfg; namespace W3DPrinter.Dal { public sealed class NHibernateHelper { private const string CurrentSessionKey = "nhibernate.current_session"; private static readonly ISessionFactory sessionFactory; static NHibernateHelper() { sessionFactory = new Configuration().Configure().BuildSessionFactory(); } public static ISession GetCurrentSession() { HttpContext context = HttpContext.Current; ISession currentSession = context.Items[CurrentSessionKey] as ISession; if (currentSession == null) { currentSession = sessionFactory.OpenSession(); context.Items[CurrentSessionKey] = currentSession; } return currentSession; } public static void CloseSession() { HttpContext context = HttpContext.Current; ISession currentSession = context.Items[CurrentSessionKey] as ISession; if (currentSession == null) { // No current session return; } currentSession.Close(); context.Items.Remove(CurrentSessionKey); } public static void CloseSessionFactory() { if (sessionFactory != null) { sessionFactory.Close(); } } } }
NHibernate的BaseDal操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NHibernate; using NHibernate.Cfg; using System.Collections; namespace W3DPrinter.Dal { public class BaseDAL { #region 查询,返回IList /// <summary> /// 查询,返回IList /// </summary> /// <param name="hql"></param> /// <returns></returns> public static IList GetObjectList(string hql) { using (ISession session = NHibernateHelper.GetCurrentSession())//打开数据库 { //查询 IQuery iquery = session.CreateQuery(hql); IList list = null; try { list = iquery.List(); } catch (Exception ex) { throw ex; } session.Close(); return list; } } #endregion #region 分页查询 /// <summary> /// 分页查询 /// </summary> /// <param name="hql"></param> /// <param name="page"></param> /// <param name="pageSize"></param> /// <returns></returns> public static IList GetObjectList(string hql, int page, int pageSize) { using (ISession session = NHibernateHelper.GetCurrentSession()) { IQuery iquery = session.CreateQuery(hql); IList list = null; try { iquery.SetFirstResult(page * pageSize);//开始 iquery.SetMaxResults(pageSize);//结束 list = iquery.List(); } catch (Exception ex) { throw ex; } session.Close(); return list; } } #endregion #region 增加一条记录 /// <summary> /// 增加一条记录 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static object Insert(object obj) { if (obj != null) { using (ISession session = NHibernateHelper.GetCurrentSession()) { ITransaction transaction = session.BeginTransaction(); object result = null; try { result = session.Save(obj); } catch (Exception ex) { throw ex; } transaction.Commit(); session.Close(); return result; } } else return null; } #endregion #region 更新 /// <summary> /// 更新 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static bool Update(object obj) { bool result = false; using (ISession session = NHibernateHelper.GetCurrentSession()) { ITransaction transaction = session.BeginTransaction(); try { session.Update(obj); transaction.Commit(); result = true; } catch (Exception ex) { throw ex; } session.Close(); return result; } } #endregion #region 删除 /// <summary> /// 删除 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static bool Delete(string hql) { bool result = false; using (ISession session = NHibernateHelper.GetCurrentSession()) { ITransaction transaction = session.BeginTransaction(); try { session.Delete(hql); transaction.Commit(); result = true; } catch (Exception ex) { throw ex; } session.Close(); return result; } } #endregion #region 执行存储过程 /// <summary> /// 执行存储过程 /// </summary> /// <param name="spname"></param> /// <param name="parms"></param> /// <param name="entityType"></param> /// <returns></returns> public static IList ExecSP(string spname, string parms, Type entityType) { using (ISession session = NHibernateHelper.GetCurrentSession()) { ITransaction transaction = session.BeginTransaction(); string sqlQuery = "Exec" + spname + " " + parms; IQuery iquery = session.CreateSQLQuery(sqlQuery).AddEntity(entityType); IList list = iquery.List(); transaction.Commit(); session.Close(); return list; } } #endregion #region 执行存储过程 /// <summary> /// 执行存储过程 /// </summary> /// <param name="spname"></param> /// <param name="parms"></param> /// <param name="entityType"></param> /// <returns></returns> public static IList ExecSP(string spname, string[] parms, Type entityType) { using (ISession session = NHibernateHelper.GetCurrentSession()) { ITransaction transaction = session.BeginTransaction(); string sqlQuery = "Exec" + spname; foreach (string p in parms) { sqlQuery += "'" + p + "',"; } sqlQuery = sqlQuery.Remove(sqlQuery.Length - 1); IQuery iquery = session.CreateSQLQuery(sqlQuery).AddEntity(entityType); IList list = iquery.List(); transaction.Commit(); session.Close(); return list; } } #endregion } }
2个类放在数据访问层,配合使用