Nhibernate基类

NHibernate是一个面向.NET环境的对象/关系数据库映射工具

1.webconfig部分

<?xml version="1.0"?>

<configuration>
  <!--配置段处理程序声明区-->
  <configSections>
    <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <!--配置段处理程序声明区-->
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  
  <!--Nhibernate连接-->
  <nhibernate>
    <add key="show-sql" value="true"/>
    <add key="hibernate.use_proxy_validator" value="false"/>
    <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
    <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
    <add key="hibernate.connection.connection_string" value="Server=.\SQLEXPRESS;User=test;Password=test;Initial Catalog=demo;integrated security=false;persist security info=True;"/>
  </nhibernate>
  <!--Nhibernate连接-->
</configuration>

 

2.读取配置文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/**/
using NHibernate;
using NHibernate.Cfg;

namespace DAL
{
    public class NHibernateSession
    {
        private static readonly ISessionFactory sessionFactory;

        #region 读取配置文件
        /// <summary>
        /// 读取配置文件
        /// </summary>
        static NHibernateSession()
        {
            Configuration cfg = new Configuration();
            cfg.AddAssembly("Model");
            sessionFactory = cfg.BuildSessionFactory();
        }
        #endregion

        public static ISessionFactory GetCurrentSessionFactory()
        {
            return sessionFactory;
        }
    }
}

3.基类操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/**/
using NHibernate;
using NHibernate.Cfg;
using System.Collections;

namespace DAL
{
   public class BaseDAL
   {
       #region 查询,返回IList
       /// <summary>
       /// 查询,返回IList
       /// </summary>
       /// <param name="hql"></param>
       /// <returns></returns>
       public static IList GetObjectList(string hql)
       {
           using (ISession session=NHibernateSession.GetCurrentSessionFactory().OpenSession())//打开数据库
           {
               //查询
               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=NHibernateSession.GetCurrentSessionFactory().OpenSession())
           {
               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 = NHibernateSession.GetCurrentSessionFactory().OpenSession())
               {
                   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=NHibernateSession.GetCurrentSessionFactory().OpenSession())
           {
              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=NHibernateSession.GetCurrentSessionFactory().OpenSession())
           {
               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=NHibernateSession.GetCurrentSessionFactory().OpenSession())
           {
               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=NHibernateSession.GetCurrentSessionFactory().OpenSession())
           {
               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
   }
}

 

posted @ 2013-01-24 18:24  摩天轮的幸福  阅读(1629)  评论(4编辑  收藏  举报