/// <summary> /// 关联查询,结果会把Customer下的Order也查出来 /// </summary> /// <param name="orderDate"></param> /// <returns></returns> public static IList<Customer> GetListByJoin(DateTime orderDate) { using (var session = Sessions.GetSession()) using (var tx = session.BeginTransaction()) { var customer = session.QueryOver<Customer>() .JoinQueryOver<DataTransfer.Order>(c => c.Orders) .Where(o => o.OrderDate == orderDate ) .List(); return customer; } }
public class SessionManger { private static readonly object lockObj = new object(); private static ISessionFactory _factory; public static ISessionFactory Factory { get { if (_factory == null) { lock (lockObj) { if (_factory == null) { NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(); _factory = cfg.BuildSessionFactory(); } } // end lock } return _factory; } } /// <summary> /// 打开session /// </summary> /// <returns></returns> public static ISession GetSession() { return Factory.OpenSession(); } /// <summary> /// 打开带连接字符串的数据库 /// </summary> /// <returns></returns> public static ISession GetSession(string connStr) { System.Data.IDbConnection conn = new System.Data.SqlClient.SqlConnection(connStr); return Factory.OpenSession(conn); } /// <summary> /// 清除缓存region /// </summary> /// <param name="region"></param> public static void ResetCacheRegion(string region) { Factory.EvictQueries(region); } /// <summary> /// /// </summary> /// <param name="sql"></param> /// <param name="paras"></param> /// <param name="session"></param> /// <returns></returns> public static NHibernate.ISQLQuery CreateSQLAndSetParam(string sql, List<NHibernate.Param.NamedParameter> paras,
NHibernate.ISession session) { var query = session.CreateSQLQuery(sql); if (paras != null) { foreach (var item in paras) { if (!string.IsNullOrEmpty(item.Name)) { query.SetParameter(item.Name, item.Value, item.Type); } } } return query; } #region 执行sql,返回IList<object[]> /// <summary> /// 执行sql,返回IList<object[]> /// </summary> /// <param name="sql">sql</param> /// <param name="paras">查询参数集合</param> /// <returns></returns> public static IList<object[]> ExecuteSQLReturnObjs(string sql) { using (var session = GetSession()) { return CreateSQLAndSetParam(sql, null, session).List<object[]>(); } } /// <summary> /// 执行sql,返回IList<object[]> /// </summary> /// <param name="sql">sql</param> /// <param name="paras">查询参数集合</param> /// <returns></returns> public static IList<object[]> ExecuteSQLReturnObjs(string sql, List<NHibernate.Param.NamedParameter> paras) { using (var session = GetSession()) { return CreateSQLAndSetParam(sql, paras, session).List<object[]>(); } } /// <summary> /// 执行sql,返回IList<object[]> /// </summary> /// <param name="sql">sql</param> /// <param name="paras">查询参数集合</param> /// <param name="session">NHibernate.ISession</param> /// <returns></returns> public static IList<object[]> ExecuteSQLReturnObjs(string sql, List<NHibernate.Param.NamedParameter> paras,
NHibernate.ISession session) { return CreateSQLAndSetParam(sql, paras, session).List<object[]>(); } /// <summary> /// 执行sql,返回IList<object[]> /// </summary> /// <param name="sql">sql</param> /// <param name="page">第几页</param> /// <param name="rows">每一页多少行</param> /// <param name="paras">查询参数集合</param> /// <returns></returns> public static IList<object[]> ExecuteSQLReturnObjs(string sql, int page, int rows) { using (var session = GetSession()) { return CreateSQLAndSetParam(sql, null, session) .SetFirstResult((page - 1) * rows) .SetMaxResults(rows) .List<object[]>(); } } #endregion #region 执行sql,返回影响的行数 /// <summary> /// 执行sql,返回影响的行数 /// </summary> /// <param name="sql"></param> /// <param name="paras"></param> /// <returns></returns> public static int ExecuteSQLReturnInt(string sql, List<NHibernate.Param.NamedParameter> paras) { using (var session = GetSession()) { return CreateSQLAndSetParam(sql, paras, session).ExecuteUpdate(); } } /// <summary> /// 执行sql,返回影响的行数 /// </summary> /// <param name="sql"></param> /// <param name="paras"></param> /// <param name="session"></param> /// <returns></returns> public static int ExecuteSQLReturnInt(string sql, List<NHibernate.Param.NamedParameter> paras, NHibernate.ISession session) { return CreateSQLAndSetParam(sql, paras, session).ExecuteUpdate(); } #endregion #region 执行sql,返回唯一属性 /// <summary> /// 执行sql,返回唯一属性 /// </summary> /// <param name="sql"></param> /// <param name="paras"></param> /// <returns></returns> public static T ExecuteSQLReturnObj<T>(string sql, List<NHibernate.Param.NamedParameter> paras) { using (var session = GetSession()) { return CreateSQLAndSetParam(sql, paras,session).UniqueResult<T>(); } } /// <summary> /// 执行sql,返回唯一属性 /// </summary> /// <param name="sql"></param> /// <param name="paras"></param> /// <param name="session"></param> /// <returns></returns> public static T ExecuteSQLReturnInt<T>(string sql, List<NHibernate.Param.NamedParameter> paras, NHibernate.ISession session) { return CreateSQLAndSetParam(sql, paras, session).UniqueResult<T>(); } #endregion }