NHibernate示例


 

1. 下載相關資源:

 

2. 下載NHibernate后解壓縮文件,看到如下文檔結構。本示例會用到Required_Bins目錄下的文件。

 

  • 下載微軟Northwind,打開SQL Server 直接運行instnwnd.sql文件的腳本就可以了。

3. 打開Visual Studio 2008。新建NHibernate.Sample解決方案。

 

4. 在新建的解決方案下創建如下新項目:

  • NHibernate.Sample.Business  【模板:類庫】;處理業務邏輯
  • NHibernate.Sample.Data  【模板:類庫】;處理數據訪問
  • NHibernate.Sample.Lib  【模板:類庫】;存放外部資源
  • NHibernate.Sample.Model  【模板:類庫】;處理業務模型
  • NHibernate.Sample.Output  【模板:控制台應用程序】;測試輸出

創建好以后,解決方案目錄如下:

 

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文件夾。

  • 在Mapping文件夾下創建Customer.hbm.xml文件:

 

  • 創建好Customer.hbm.xml文件以后,在打開的編輯界面內,右鍵=》屬性。
  • 打開屬性管理窗口:

 

  • 在屬性管理窗口的架構一項,選擇架構文件。架構文件的地址,就是我們上一步已經拷貝到NHibernate.Sample.Lib項目中的nhibernate-mapping.xsd文件。添加好以后編輯Customer.hbm.xml文件,就更夠通過Visual Studio智能所用到的配置項。

 

  • 在解決方案管理器中找到Customer.hbm.xml文件,右鍵=》屬性

 

  • 把“生成操作”屬性改為:嵌入的資源。

  

  • 完成Customer.hbm.xml文件內容:【注意:assembly和namespace屬性】
<?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>
  • 添加Customer.cs文件,完成Customer類:
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; }
    }
}
  • 創建好以后NHibernate.Sample.Model項目的目錄結構如下:

 

7. NHibernate.Sample.Data項目,用來數據訪問。創建文件夾Config,用來存放配置文件,創建數據訪問基類,創建數據訪問接口,創建數據訪問類。

  • 創建NHiberane的配置文件hibernate.cfg.xml:

 

  • 設定hibernate.cfg.xml文件的框架,在文件的編輯窗口右鍵=》屬性=》添加=》選擇Schema文件(NHibernate.Sample.Lib項目中的nhibernate-configuration.xsd文件)。

 

  • 完成hibernate.cfg.xml文件的內容:
<?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>
  • 添加BaseOperator.cs文件,完成BaseOperator類:
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(); 
        }
    }
}
  • 添加CustomerOperator.cs文件,完成CustomerOperator類:
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();
                return id;
        }

        public void Update(Customer customer)
        {
            Session.Update(customer);
            Session.Flush();
        }

        public void Delete(Customer customer)
        {
            Session.Delete(customer);
            Session.Flush();
        }

        public Customer Get(object id)
        {
            return Session.Get<Customer>(id);
        }

        public IList<Customer> GetAll()
        {
            return Session.Query<Customer>().ToList();
        }

    }
}
  • 完成后的NHibernate.Sample.Data目錄結構如下:

 

8. NHibernate.Sample.Business項目用來,處理業務邏輯;本示例比較簡單,我們只添加一個邏輯處理類CustomerLogic,內容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate.Sample.Data;
using NHibernate.Sample.Model;

namespace NHibernate.Sample.Business
{
    public class CustomerLogic
    {
        public IList<Customer> GetAll()
        {
            CustomerOperator oper = new CustomerOperator();
            return oper.GetAll();
        }
    }
}

 

  • 完成以后項目結構如下:

 

9. NHibernate.Sample.Output為控制台項目,用來測試輸出。添加一個配置文件App.config用於連接數據庫,內容視個人環境而定。

App.config內容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Connection String" connectionString="Data Source=.\SQLExpress;Initial Catalog=Northwind;Integrated Security=SSPI;" />
  </connectionStrings>
</configuration>
  • Program文件內容如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Sample.Business;
using NHibernate.Sample.Model;

namespace NHibernate.Sample.Output
{
    class Program
    {
        static void Main(string[] args)
        {
            GetAll();
        }

        private static void GetAll()
        {
            CustomerLogic logic = new CustomerLogic();
            IList<Customer> cList = logic.GetAll();

            foreach (Customer item in cList)
            {
                Console.WriteLine(item.CustomerID.ToString());
            }
            Console.Read();
        }
    }
}
  • NHibernate.Sample.Output為控制台項目下,E:\Study\NHibernate.Sample\NHibernate.Sample.Output\bin\Debug應包含如下文件。否則無法運行成功;如果缺少去其他項目中拷貝一份過來就可以了。

 

10. 運行結果如下:

 

11. 代碼下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM