1. 下载相关资源:
2. 下载NHibernate后解压缩文件,看到如下文档结构。本示例会用到Required_Bins目录下的文件。
3. 打开Visual Studio 2008。新建NHibernate.Sample解决方案。
4. 在新建的解决方案下创建如下新项目:
创建好以后,解决方案目录如下:
5. NHibernate.Sample.Lib项目,用来统一存放本示例用到的所有外部资源。创建三个个文件夹,分别为Dll、Schema、DBScript;分别用来存放NHibernate相关Dll文件、Schema文件和示例用到的数据库脚本文件。下面需要把相应的文件拷贝到对应的文件夹下。【这一步可以做,也可以不做。一般在真实的项目都会做,方便统一管理】。
完成的NHibernate.Sample.Lib项目结构如下:
instnwnd.sql文件来自下, 载微软Northwind示例数据库。第一步已经提供了下载地址。Iesi.Collections.dll、NHibernate.dll、nhibernate-configuration.xsd、nhibernate-mapping.xsd四个文件都来自下载的NHibernate,Required_Bins文件目录下。
6. NHibernate.Sample.Model项目,用来创建模型,对应于数据库中的表。添加Customer类,添加Mapping文件夹。
<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Sample.Model" namespace="NHibernate.Sample.Model"> <class name="Customer" table="Customers" lazy="true"> <id name="CustomerID" column="CustomerID" type="string"/> <PRoperty name="CompanyName" type="string"> <column name="CompanyName" length="40"/> </property> <property name="ContactName" type="string"> <column name="ContactName" length="30"/> </property> <property name="ContactTitle" type="string"> <column name="ContactTitle" length="30"/> </property> <property name="Address" type="string"> <column name="Address" length="60"/> </property> <property name="City" type="string"> <column name="City" length="15"/> </property> <property name="Region" type="string"> <column name="Region" length="15"/> </property> <property name="PostalCode" type="string"> <column name="PostalCode" length="10"/> </property> <property name="Country" type="string"> <column name="Country" length="15"/> </property> <property name="Phone" type="string"> <column name="Phone" length="24"/> </property> <property name="Fax" type="string"> <column name="Fax" length="24"/> </property> </class></hibernate-mapping>
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace NHibernate.Sample.Model{ public class Customer { /// <summary> /// /// </summary> public virtual string CustomerID { get; set; } /// <summary> /// /// </summary> public virtual string CompanyName { get; set; } /// <summary> /// /// </summary> public virtual string ContactName { get; set; } /// <summary> /// /// </summary> public virtual string ContactTitle { get; set; } /// <summary> /// /// </summary> public virtual string Address { get; set; } /// <summary> /// /// </summary> public virtual string City { get; set; } /// <summary> /// /// </summary> public virtual string Region { get; set; } /// <summary> /// /// </summary> public virtual string PostalCode { get; set; } /// <summary> /// /// </summary> public virtual string Country { get; set; } /// <summary> /// /// </summary> public virtual string Phone { get; set; } /// <summary> /// /// </summary> public virtual string Fax { get; set; } }}
7. NHibernate.Sample.Data项目,用来数据访问。创建文件夹Config,用来存放配置文件,创建数据访问基类,创建数据访问接口,创建数据访问类。
<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string_name">Connection String</property> <property name="connection.isolation">ReadCommitted</property> <property name="show_sql">false</property> <mapping assembly="NHibernate.Sample.Model"/> </session-factory></hibernate-configuration>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using NHibernate;using NHibernate.Cfg;namespace NHibernate.Sample.Data{ public class BaseOperator { private ISession m_Session; public ISession Session { get { return m_Session;} } private ISessionFactory m_SessionFactory; public BaseOperator() { var config = new Configuration().Configure("Config/hibernate.cfg.xml"); m_SessionFactory = config.BuildSessionFactory(); m_Session = m_SessionFactory.OpenSession(); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using NHibernate.Linq;using NHibernate.Sample.Model;namespace NHibernate.Sample.Data{ public class CustomerOperator : BaseOperator { public object Save(Customer customer) { var id = Session.Save(customer); Session.Flush(); retur
新闻热点
疑难解答