配置ASP.NET Nhibernate
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的更多相关文章
- 在Linux(Ubuntu/openSUSE/CentOS)下配置ASP.NET(Apache + Mono)
[题外话] 闲的无聊竟然想尝试测试自己做的项目在不同操作系统上的性能表现,所以决定试试在Linux上部署Apache和Mono的环境.由于平时很少接触Linux,所以从网上找了几篇文章(附在相关链接中 ...
- 用"hosting.json"配置ASP.NET Core站点的Hosting环境
通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program { p ...
- [原创]IIS7.5下配置ASP+PHP环境及错误处理(0xc0000135)
IIS7.5下配置ASP+PHP环境及错误处理(0xc0000135) http://user.qzone.qq.com/93701178/blog/1398155812 操作系统更新至Win7或Wi ...
- 配置ASP.NET Web应用程序, 使之运行在medium trust
这文章会向你展示, 怎么配置ASP.NET Web应用程序, 使之运行在medium trust. 如果你的服务器有多个应用程序, 你可以使用code access security和medium ...
- 如何在IIS7下配置ASP+ACCESS环境
如何在IIS7下配置ASP+ACCESS环境 | 浏览:901 | 更新:2013-01-16 17:46 1 2 3 4 5 6 7 分步阅读 默认装完IIS7之后,使用ASP程序会发现提示数据库连 ...
- IIS配置ASP.NET和服务器错误页
以下两种方法均为全站出错处理 方法一: 1.在Web.config配置文件中<system.web></system.web>中添加<customErrors mode= ...
- Win7旗舰版中的IIS配置asp.net的运行环境
Win7旗舰版中的IIS配置asp.net的运行环境 以前弄过好多次,都没有成功,昨天晚上不知怎么地就成功了,借用我同学的一句话,这叫“灵光一闪”,废话不多说了,这个成功是有图有视频有真相地哈! ...
- window下appserv组合包配置asp标记风格与简短风格
php一共有四种编码风格 分别为 :XML风格,脚本分铬,简短风格,ASP风格 如果要配置asp标记风格与简短风格,需要在php.ini文件中配置. 打开文件的位置C:\ window\php.ini ...
- 图解win7中IIS7.0的安装及配置ASP环境
控制面板中“程序”的位置 “程序”中“打开或关闭Windows功能”的位置 如图,安装IIS7时需要选择要使用的功能模块 IIS7安装完成之后可以在开始菜单的所有程序中看到“管理工具”,其中有一个“I ...
随机推荐
- Bootstrap 基本用法(续)
在bootstrap中有很多的组件,这些组件可以帮组我们更快的写出一些好看的样式,下面就是一些样式: 导航框: <ul class="nav nav-tabs"> &l ...
- Table of Contents - Spring
The IoC container Spring 容器 属性注入 & 构造注入 Bean 实例的创建方式 p-namespace & c-namespace 集合属性的注入 作用域 延 ...
- Java开发从零开始填坑
开始学习Java,感觉较.NET知识更零碎一些,所以开个帖子把自己踩过的坑记录下来,都是边边角角网上不容易找到的东西. 1.java命令格式:>cd %parent-of-pakadgePath ...
- android app性能优化大汇总(google官方Android性能优化典范 - 第1季)
大多数用户感知到的卡顿等性能问题的最主要根源都是因为渲染性能.从设计师的角度,他们希望App能够有更多的动画,图片等时尚元素来实现流畅的用户体验.但是Android系统很有可能无法及时完成那些复杂的界 ...
- PB出现中文乱码
在design>option>font把字体都设定为宋体 tools>system option>font 把字体都设定为宋体 然后重启PB即可
- Zend studio 12.5.1安装aptana
aptana是zend studio的一个插件.解决zend对于前台html支持,加亮的问题. 安装方法其实很简单 ,直接给出aptana的地址了. http://download.aptana.co ...
- OSPF LSA的详解
LSA类型的配置与查看 1基本配置 R1(config)#NO IP DO LO R1(config)#NO ENAble PAssword R1(config)#LINe COnsole 0 R1( ...
- JavaScript语言标识符和保留字
任何一种计算机语言都离不开标识符和保留字,下面我们将详细介绍JavaScript标识符和关键字.标识符 标识符就是给变量.函数和对象等指定的名字.构成标识符的字母是有一定的规范,JavaSc ...
- Linux多线程编程(不限Linux)
前言 线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步.互斥,这些东西将在本文中介绍.我在某QQ群里见到这样一道面试题: 是否熟 ...
- 简明Python中的一个小错误
最近在学Python,先看的是<Python基础教程>,后来经别人推荐,感觉网络上的<简明Python教程>也挺好的,在里面发现一个小错误. 网址如下:http://sebug ...