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. part 4 AngularJS ng src directive

  2. [转] 利用任务计划重启sqlserver服务

    1.建立一个批处理文件restartsqlserver.bat     内容如下:     net   stop   mssqlserver   /y    net   start  mssqlser ...

  3. 【leetcode】13. Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...

  4. UILabel常用属性

    [super viewDidLoad]; // 实例化UILabel并指定其边框 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0 ...

  5. <Linux系统hostname命令详解>

    hostname命令的用法的小知识我们都知道hostname命令是查看主机名和修改主机名的. [root@apache ~]# hostname  //查看本机的主机名apache.example.c ...

  6. 使用C++读取UTF8及GBK系列的文本方法及原理

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4374404.html 1.读取UTF-8编码文本原理 首先了解UTF-8的编码方式,UTF- ...

  7. 理解 pkg-config 工具

    引用了别人的文章:http://www.chenjunlu.com/2011/03/understanding-pkg-config-tool/ 你在 Unix 或 Linux 下开发过软件吗?写完一 ...

  8. [大牛翻译系列]Hadoop(16)MapReduce 性能调优:优化数据序列化

    6.4.6 优化数据序列化 如何存储和传输数据对性能有很大的影响.在这部分将介绍数据序列化的最佳实践,从Hadoop中榨出最大的性能. 压缩压缩是Hadoop优化的重要部分.通过压缩可以减少作业输出数 ...

  9. ASP.NET中的TextBox下划线

    看到园子里一位同行写的 http://www.cnblogs.com/xiaopeng84/archive/2007/04/10/707093.html 但是没有贴出效果图,自己练了一下,贴出代码和效 ...

  10. 用“逐步排除”的方法定位Java服务线上“系统性”故障(转)

    一.摘要 由于硬件问题.系统资源紧缺或者程序本身的BUG,Java服务在线上不可避免地会出现一些“系统性”故障,比如:服务性能明显下降.部分(或所 有)接口超时或卡死等.其中部分故障隐藏颇深,对运维和 ...