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

首先介绍下我的开发环境是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>(); 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配置的更多相关文章

  1. Maven整合Spring3.0+Mybatis3.2+Struts2.3+查找坐标+jar包依赖(五)

    依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar,这种情况 叫 依赖传递

  2. QQ登录整合/oauth2.0认证-03-对第二节的代码改进

    ---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...

  3. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口(转)

    转自:[CXF REST标准实战系列] 二.Spring4.0 整合 CXF3.0,实现测试接口 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现W ...

  4. Struts2(五.用户注册的实现及整合Action的配置方法)

    一.用户注册功能 register.jsp页面 若是jquery ajax方式提交给action,还要回到jquery,控制权在jquery若是表单方式提交给action,控制权交给action &l ...

  5. Nginx缩略图和Fastdfs整合以及image_filter配置,7点经验结论和5个参考资料

    以下是7点经验结论和5个参考资料 1.Nginx单独配置缩略图与Nginx和Fastdfs模块整合,配置是不一样的. 非整合模式的配置,类似这样的:  location ~* /(\d+)\.(jpg ...

  6. SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂)

    SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂) 一,整合Swagger3.0 随着Spring Boot.Spring Cloud等微服务的 ...

  7. Swagger整合Jwt授权配置

    Swagger整合Jwt授权配置 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章http://www.javaman.cn/sb2/swagger-jwt 一.Swagger入门 ...

  8. IIS运行.NET4.0配置

    IIS运行.NET4.0配置 “/CRM”应用程序中的服务器错误.配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法 ...

  9. kafka0.9.0及0.10.0配置属性

    问题导读1.borker包含哪些属性?2.Producer包含哪些属性?3.Consumer如何配置?borker(0.9.0及0.10.0)配置Kafka日志本身是由多个日志段组成(log segm ...

随机推荐

  1. Hadoop MapReduce中压缩技术的使用

    Compression and Input Splits   当我们使用压缩数据作为MapReduce的输入时,需要确认数据的压缩格式是否支持切片?   假设HDFS中有一个未经压缩的大小为1GB的文 ...

  2. HDOJ 2206 IP的计算(正则表达式的应用)

    Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方 ...

  3. objc_msgSend iOS8 EXC_BAD_ACCESS

    如果方法是没有返回值的,需要强转一个返回类型为void的临时函数指针, void (*objc_msgSendTyped)(id self, SEL _cmd, id obj, id arg1) = ...

  4. Windows Live Writer Install Faied

    win7安装Windows Live Writer软件时失败,并且提示错误代码0x80190194的问题 Windows Live Writer是一个很好的博客写作工具,单机就可以使用了,还可以用于同 ...

  5. 一般处理程序中使用Session出现未将对象引用设置到对象的实例

    遇到问题:未将对象引用设置到对象的实例 那就在你的一般处理程序中加入红色背景的代码吧 using System; using System.Collections.Generic; using Sys ...

  6. const和violate

    const修饰变量 C语言中,const修饰的变量是只读的,本质还是变量,修饰的变量也会在内存中占用空间(这不废话么)本质上const只对编译器有用,在运行时无用.const和类型顺序无要求,一般类型 ...

  7. iOS CoreBluetooth 教程

    去App Store搜索并下载“LightBlue”这个App,对调试你的app和理解Core Bluetooth会很有帮助. ================================ Cor ...

  8. Android SimpleAdapter ListView (锁定手机,解锁手机的列表)

    SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局. 构造方法: SimpleAdapter(Context context, List<? extends Map< ...

  9. 杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)

    欢迎"热爱编程"的高考少年--报考杭州电子科技大学计算机学院 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memor ...

  10. Android中解析网络请求的URL

    近期正在做Android网络应用的开发,使用了android网络请求方面的知识.如今向大家介绍网络请求方面的知识.我们知道android中向server端发送一个请求,(这就是我们通常所说的POST请 ...