web.config:配置sql server数据库

 <configuration>
<configSections>
<!--NHibernate Section-->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
<!--NHibernate Section End-->
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
server=127.0.0.1;database=NHibernateSample;uid=sa;pwd=zhangwei
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
</property>
</session-factory>
</hibernate-configuration>
......

web.config:配置 Oracle 数据库

 <configuration>
<configSections>
<!--NHibernate Section-->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
<!--NHibernate Section End-->
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
NHibernate.Driver.OracleClientDriver
</property>
<property name="dialect">
NHibernate.Dialect.Oracle10gDialect
</property>
<property name="connection.connection_string">
Data Source=155ZNXJ;User ID=cxm;Password=cxm
</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHibernate.ByteCode.LinFu
</property>
<property name="show_sql">true</property>
<property name="query.substitutions">
true 1, false 0, yes 'Y', no 'N'
</property>
</session-factory>
</hibernate-configuration>
......

SessionFactory:

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg; namespace MyMvcDemo.Nhibernate
{
public class SessionFactory
{
private ISessionFactory _sessionFactory; public SessionFactory()
{
_sessionFactory = GetSessionFactory();
} private void Init()
{
var config = new Configuration();
config.AddAssembly("MyMvcDemo.Nhibernate");
config.Configure();
_sessionFactory = config.BuildSessionFactory();
} private ISessionFactory GetSessionFactory()
{
if (_sessionFactory == null)
Init(); return _sessionFactory;
} public ISession GetSession()
{
return _sessionFactory.OpenSession();
}
}
}

Customer.hbm.xml:

 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyMvcDemo.Model" namespace="MyMvcDemo.Model.Customer">
<class name ="Customer" table="Customer" lazy="false">
<id name="CustomerId">
<generator class ="native"/>
</id> <property name="FirstName"/>
<property name ="LastName"/>
</class>
</hibernate-mapping>

Customer.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyMvcDemo.Model.Customer
{
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Version { get; set; }
}
}

HomeController.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using NHibernate;
using NHibernate.Cfg;
using MyMvcDemo.Service;
using MyMvcDemo.Nhibernate; namespace MyMvcDemo.Controllers
{
public class HomeController : Controller
{
private readonly ISession _session;
readonly SessionFactory _sessionFactory = new SessionFactory();
private readonly CustomerService _customerService; public HomeController()
{
_session = _sessionFactory.GetSession();
_customerService = new CustomerService(_session);
} public ActionResult Index()
{
var customer = _customerService.GetCustomerById();
ViewData["Message"] = customer.FirstName + customer.LastName;
return View();
} public ActionResult About()
{
ViewData["Message"] = "Hello About";
return View();
}
}
}

CustomerService.cs:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using MyMvcDemo.Nhibernate;
using MyMvcDemo.Model.Customer;
using MyMvcDemo.Service;
using NHibernate.Criterion; namespace MyMvcDemo.Service
{
public class CustomerService
{
private ISession _session;
public ISession Session
{
set { _session = value; }
} /// <summary>
/// 初始化Session
/// </summary>
/// <param name="session"></param>
public CustomerService(ISession session)
{
_session = session;
} /// <summary>
/// 添加Customer
/// </summary>
/// <param name="customer"></param>
public void CreateCustomer(Customer customer)
{
_session.Save(customer);
_session.Flush();
} /// <summary>
/// 根据id获取Customer
/// </summary>
/// <param name="customerid"></param>
/// <returns></returns>
public Customer GetCustomerById(int customerid)
{
return _session.Get<Customer>(customerid);
} public IList<Customer> GetCustomerByFirstName(string name)
{
return _session.CreateCriteria(typeof(Customer))
.Add(Restrictions.Eq("FirstName", name))
.List<Customer>();
}
}
}

Index.aspx:

 <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: ViewData["Message"] %></h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
</asp:Content>

运行结果:

配置ASP.NET Nhibernate的更多相关文章

  1. 在Linux(Ubuntu/openSUSE/CentOS)下配置ASP.NET(Apache + Mono)

    [题外话] 闲的无聊竟然想尝试测试自己做的项目在不同操作系统上的性能表现,所以决定试试在Linux上部署Apache和Mono的环境.由于平时很少接触Linux,所以从网上找了几篇文章(附在相关链接中 ...

  2. 用"hosting.json"配置ASP.NET Core站点的Hosting环境

    通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program { p ...

  3. [原创]IIS7.5下配置ASP+PHP环境及错误处理(0xc0000135)

    IIS7.5下配置ASP+PHP环境及错误处理(0xc0000135) http://user.qzone.qq.com/93701178/blog/1398155812 操作系统更新至Win7或Wi ...

  4. 配置ASP.NET Web应用程序, 使之运行在medium trust

    这文章会向你展示, 怎么配置ASP.NET Web应用程序, 使之运行在medium trust.   如果你的服务器有多个应用程序, 你可以使用code access security和medium ...

  5. 如何在IIS7下配置ASP+ACCESS环境

    如何在IIS7下配置ASP+ACCESS环境 | 浏览:901 | 更新:2013-01-16 17:46 1 2 3 4 5 6 7 分步阅读 默认装完IIS7之后,使用ASP程序会发现提示数据库连 ...

  6. IIS配置ASP.NET和服务器错误页

    以下两种方法均为全站出错处理 方法一: 1.在Web.config配置文件中<system.web></system.web>中添加<customErrors mode= ...

  7. Win7旗舰版中的IIS配置asp.net的运行环境

    Win7旗舰版中的IIS配置asp.net的运行环境   以前弄过好多次,都没有成功,昨天晚上不知怎么地就成功了,借用我同学的一句话,这叫“灵光一闪”,废话不多说了,这个成功是有图有视频有真相地哈! ...

  8. window下appserv组合包配置asp标记风格与简短风格

    php一共有四种编码风格 分别为 :XML风格,脚本分铬,简短风格,ASP风格 如果要配置asp标记风格与简短风格,需要在php.ini文件中配置. 打开文件的位置C:\ window\php.ini ...

  9. 图解win7中IIS7.0的安装及配置ASP环境

    控制面板中“程序”的位置 “程序”中“打开或关闭Windows功能”的位置 如图,安装IIS7时需要选择要使用的功能模块 IIS7安装完成之后可以在开始菜单的所有程序中看到“管理工具”,其中有一个“I ...

随机推荐

  1. WEB系统架构

    客户端方向:框架+控件+模板+元数据辅助:懒加载+合并请求+异步任务+推送+缓存技术:reactjs,requirejs,jquery,angularjs,bootstrap,ant.design,f ...

  2. ubuntu安装python3

    系统本身就已经安装了python2.7 和python3.4 现在需要做的就是将默认的版本更换一下下就可以了. 检查python的版本 python -V 老的版本没必要去产出了,因为会有一些程序依赖 ...

  3. C# 类构造函数赋值里属性与字段赋值注意项

    public class Test { public Test(int age) { this.Age=age;//如果这里使用的是this.age=age;那么属性里的判断将不会执行 } priva ...

  4. 初识iOS9 iPad新特性SlideView和SplitView的适配

    苹果刚发布了iOS9,在iPad上新增了两个新的特性SlideView和SplitView,前者可以在不关闭当前激活APP的情况下调出来另外个APP以30%比例显示进行操作使用,后者允许同时运行两个A ...

  5. 推荐7款新鲜出炉的HTML5/CSS3应用

    1.HTML5/CSS3发光文字可自定义文字色彩效果很赞 要分享的这款HTML5/CSS3文字效果很帅,鼠标滑过文字时,文字会出现发光的特效,并且我们可以自定义文字和颜色. 在线演示 源码下载 2.H ...

  6. 基类和派生类--this

    基类指针在程序运行的时候的确指向的是一个派生类的对象,但指针的类型仍然是基类指针.C++是一种强类型语言,因此不能用基类指针类型的指针直接调用派生类:而且,同一个类可能有多种不同的派生类,因此不知道实 ...

  7. iOS Foundation框架简介 -1.常用结构体的用法和输出

    1.安装Xcode工具后会自带开发中常用的框架,存放的地址路径是: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.plat ...

  8. Linux C 程序 Linux网络编程(21)

    Linux网络编程网络编程必备的理论基础网络模型,地址,端口,TCP/IP协议 TCP/IP协议是目前世界上使用最广泛的网络通信协议日常中的大部分应用使用该系列协议(浏览网页,收发电子邮件,QQ聊天等 ...

  9. module graceful-fs for npm

    问题: 今天使用hexo时发现错误,hexo:command not found.于是重新安装hexo.但是在安装好npm后,却发现运行 npm 出现错误,没有找到模块graceful-fs,在纠结了 ...

  10. 如何在java类中读取Properties配置文件

    在com.example包下有一个test.properties文件和测试类PropertyReadTest.java. test.properties 文件内容: author=zeige  tea ...