mvc4 整合nhibernate3.0配置

鉴于大家都在解决问题或是学习新东西的时候,并不关注是谁又是谁帮你解决了问题,所有这里为自己做下宣传,我为自己代言。

首先介绍下我的开发环境是vs2010旗舰版,nhibernate采用的是3.0版本。

一、在mvc4项目中装载配置nhibernate的第一种方式是采用【hibernate.xml】的方式:首先看图

使用这种方式配置nhibernate的话,需要将hibernate.xml【属性】改为始终复制到输入目录或作为嵌入资源使用。

当然要使用nhibernate的化,需要在项目中引入hibernate.dll.

接下来我们来看下,在代码中如何来装载我们对nhibernate的配置:

 

public class NhibernateHelper
    {
        private ISessionFactory _sessionFactory;
        private static ISession _session = null; 
        public NhibernateHelper()
        {
            _sessionFactory = GetSessionFactory();
        }
        public ISessionFactory GetSessionFactory()
        {
            //加载文件的方式
            Configuration cfg = new Configuration().Configure(AppDomain.CurrentDomain.BaseDirectory+"/hibernate.cfg.xml");
            //Configuration cfg = new Configuration();
            //cfg.AddAssembly(Assembly.GetExecutingAssembly());
          
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();//NHProf工具

            cfg.AddAssembly("BusinessDemo");//BusinessDemo指的是包含数据表的dll类库如:user.hbm.xml

            //cfg.AddAssembly(typeof(User).Assembly);
            //cfg.AddAssembly(typeof(Order).Assembly);
            return cfg.BuildSessionFactory();
        }
        public ISession GetSession()
        {
            if (_session != null && _session.IsOpen)
                return _session;
            else
            {
                _session = _sessionFactory.OpenSession();
                return _session;
            }
        }
    }

 然后我们在mvc4 Controller里边就可以这样来做下测试:

    public class CustomerController : Controller{

        public ISession session { get { return new NhibernateHelper().GetSession(); } }

        public ActionResult Index()
        {
            Customer customer = this.session.Load<Customer>(2);

            var result = new Result();
            using (ITransaction tx = this.session.BeginTransaction())
            {
                try
                {
                    this.session.Delete(customer);
                    this.session.Flush();
                    tx.Commit();
                    result.success = true;
                    result.msg = "删除成功!";
                                       
                }
                catch (HibernateException)
                {
                    tx.Rollback();
                    result.success = true;
                    result.msg = "删除失败!";
                }
            }
            return Json(result, JsonRequestBehavior.AllowGet);  
        }


    }

二、接下来我们来看如何在么mvc项目中如何在web.config中配置nhibernate信息:直接上代码

<configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
  </configSections>
    <connectionStrings>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
      <property name="connection.connection_string">Database=test;Data Source=localhost;User Id=root;Password=root</property>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property>
    </session-factory>
  </hibernate-configuration>

注: <property name="dialect"> 、<property name="connection.driver_class">这两个属性一定要和链接的数据库保持一致,这里边我采用的是mysql数据库。

然后我们直接来看如何来装载它:

public class NhibernateHelper
    {
        private ISessionFactory _sessionFactory;
        private static ISession _session = null; 
        public NhibernateHelper()
        {
            _sessionFactory = GetSessionFactory();
        }
        public ISessionFactory GetSessionFactory()
        {
            //加载文件的方式
            //Configuration cfg = new Configuration().Configure(AppDomain.CurrentDomain.BaseDirectory+"/hibernate.cfg.xml");
            Configuration cfg = new Configuration();
            cfg.AddAssembly(Assembly.GetExecutingAssembly());
          
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();//NHProf工具

            //cfg.AddAssembly("BusinessDemo");

            //cfg.AddAssembly(typeof(User).Assembly);
            //cfg.AddAssembly(typeof(Order).Assembly);
            return cfg.BuildSessionFactory();
        }
        public ISession GetSession()
        {
            if (_session != null && _session.IsOpen)
                return _session;
            else
            {
                _session = _sessionFactory.OpenSession();
                return _session;
            }
        }
    }

 

posted @ 2015-02-10 08:39  fullSunlight  阅读(922)  评论(1编辑  收藏  举报