码迷,mamicode.com
首页 > Web开发 > 详细

mvc4 整合nhibernate3.0配置

时间:2015-02-10 09:10:54      阅读:722      评论:0      收藏: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;
            }
        }
    }

 

mvc4 整合nhibernate3.0配置

标签:

原文地址:http://www.cnblogs.com/gaolizhong/p/4282961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!