配置ASP.NET Nhibernate
web.config:配置sql server数据库
1 <configuration> 2 <configSections> 3 <!--NHibernate Section--> 4 <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/> 5 <!--NHibernate Section End--> 6 </configSections> 7 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 8 <session-factory name="NHibernate.Test"> 9 <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 10 <property name="connection.connection_string"> 11 server=127.0.0.1;database=NHibernateSample;uid=sa;pwd=zhangwei 12 </property> 13 <property name="adonet.batch_size">10</property> 14 <property name="show_sql">true</property> 15 <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> 16 <property name="use_outer_join">true</property> 17 <property name="command_timeout">60</property> 18 <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> 19 <property name="proxyfactory.factory_class"> 20 NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu 21 </property> 22 </session-factory> 23 </hibernate-configuration> 24 ......
web.config:配置 Oracle 数据库
1 <configuration> 2 <configSections> 3 <!--NHibernate Section--> 4 <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/> 5 <!--NHibernate Section End--> 6 </configSections> 7 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 8 <session-factory> 9 <property name="connection.provider"> 10 NHibernate.Connection.DriverConnectionProvider 11 </property> 12 <property name="connection.driver_class"> 13 NHibernate.Driver.OracleClientDriver 14 </property> 15 <property name="dialect"> 16 NHibernate.Dialect.Oracle10gDialect 17 </property> 18 <property name="connection.connection_string"> 19 Data Source=155ZNXJ;User ID=cxm;Password=cxm 20 </property> 21 <property name="proxyfactory.factory_class"> 22 NHibernate.ByteCode.LinFu.ProxyFactoryFactory, 23 NHibernate.ByteCode.LinFu 24 </property> 25 <property name="show_sql">true</property> 26 <property name="query.substitutions"> 27 true 1, false 0, yes 'Y', no 'N' 28 </property> 29 </session-factory> 30 </hibernate-configuration> 31 ......
SessionFactory:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using NHibernate; 7 using NHibernate.Cfg; 8 9 namespace MyMvcDemo.Nhibernate 10 { 11 public class SessionFactory 12 { 13 private ISessionFactory _sessionFactory; 14 15 public SessionFactory() 16 { 17 _sessionFactory = GetSessionFactory(); 18 } 19 20 private void Init() 21 { 22 var config = new Configuration(); 23 config.AddAssembly("MyMvcDemo.Nhibernate"); 24 config.Configure(); 25 _sessionFactory = config.BuildSessionFactory(); 26 } 27 28 private ISessionFactory GetSessionFactory() 29 { 30 if (_sessionFactory == null) 31 Init(); 32 33 return _sessionFactory; 34 } 35 36 public ISession GetSession() 37 { 38 return _sessionFactory.OpenSession(); 39 } 40 } 41 }
Customer.hbm.xml:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyMvcDemo.Model" namespace="MyMvcDemo.Model.Customer"> 3 <class name ="Customer" table="Customer" lazy="false"> 4 <id name="CustomerId"> 5 <generator class ="native"/> 6 </id> 7 8 <property name="FirstName"/> 9 <property name ="LastName"/> 10 </class> 11 </hibernate-mapping>
Customer.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyMvcDemo.Model.Customer 7 { 8 public class Customer 9 { 10 public int CustomerId { get; set; } 11 public string FirstName { get; set; } 12 public string LastName { get; set; } 13 public string Version { get; set; } 14 } 15 }
HomeController.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web.Mvc; 6 using NHibernate; 7 using NHibernate.Cfg; 8 using MyMvcDemo.Service; 9 using MyMvcDemo.Nhibernate; 10 11 namespace MyMvcDemo.Controllers 12 { 13 public class HomeController : Controller 14 { 15 private readonly ISession _session; 16 readonly SessionFactory _sessionFactory = new SessionFactory(); 17 private readonly CustomerService _customerService; 18 19 public HomeController() 20 { 21 _session = _sessionFactory.GetSession(); 22 _customerService = new CustomerService(_session); 23 } 24 25 26 public ActionResult Index() 27 { 28 var customer = _customerService.GetCustomerById(1); 29 ViewData["Message"] = customer.FirstName + customer.LastName; 30 return View(); 31 } 32 33 public ActionResult About() 34 { 35 ViewData["Message"] = "Hello About"; 36 return View(); 37 } 38 } 39 }
CustomerService.cs:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using NHibernate.Cfg; 6 using NHibernate; 7 using MyMvcDemo.Nhibernate; 8 using MyMvcDemo.Model.Customer; 9 using MyMvcDemo.Service; 10 using NHibernate.Criterion; 11 12 namespace MyMvcDemo.Service 13 { 14 public class CustomerService 15 { 16 private ISession _session; 17 public ISession Session 18 { 19 set { _session = value; } 20 } 21 22 /// <summary> 23 /// 初始化Session 24 /// </summary> 25 /// <param name="session"></param> 26 public CustomerService(ISession session) 27 { 28 _session = session; 29 } 30 31 /// <summary> 32 /// 添加Customer 33 /// </summary> 34 /// <param name="customer"></param> 35 public void CreateCustomer(Customer customer) 36 { 37 _session.Save(customer); 38 _session.Flush(); 39 } 40 41 /// <summary> 42 /// 根据id获取Customer 43 /// </summary> 44 /// <param name="customerid"></param> 45 /// <returns></returns> 46 public Customer GetCustomerById(int customerid) 47 { 48 return _session.Get<Customer>(customerid); 49 } 50 51 public IList<Customer> GetCustomerByFirstName(string name) 52 { 53 return _session.CreateCriteria(typeof(Customer)) 54 .Add(Restrictions.Eq("FirstName", name)) 55 .List<Customer>(); 56 } 57 } 58 }
Index.aspx:
1 <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 2 3 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 4 Home Page 5 </asp:Content> 6 7 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 8 <h2><%: ViewData["Message"] %></h2> 9 <p> 10 To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>. 11 </p> 12 </asp:Content>
运行结果: