• Nhibernate笔记2


    /// <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 }
    用对方法才有效率,做对事情才有效果
    “麻烦”是自己“处理”不当的结果
    “困难”是自己“学习”不够的反射

    “挫折”是自己“努力”不足的代价

  • 相关阅读:
    讲清楚之 javascript 参数传值
    JS+CSS3 360度全景图插件
    一些个人感觉很不错的特效
    从ES6重新认识JavaScript设计模式(三): 建造者模式
    动态监听输入框值的变化
    React Native基础&入门教程:调试React Native应用的一小步
    这儿有一个使你网页性能提升10倍的工具
    业务连续性实战
    strong and weak 强引用和弱引用的差别
    Codeforces Round #254 (Div. 2):B. DZY Loves Chemistry
  • 原文地址:https://www.cnblogs.com/ly7454/p/2991032.html
Copyright © 2020-2023  润新知