国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

NHibernate示例

2019-11-15 02:30:18
字體:
來源:轉載
供稿:網友

NHibernate示例

1. 下載相關資源:

  • 下載NHibernate。下載地址: http://nhforge.org/Default.aspx

  • 下載微軟Northwind示例數據庫,下載地址:http://www.microsoft.com/en-us/download/details.aspx?id=23654

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

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

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

4. 在新建的解決方案下創(chuàng)建如下新項目:

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

創(chuàng)建好以后,解決方案目錄如下:

5. NHibernate.Sample.Lib項目,用來統(tǒng)一存放本示例用到的所有外部資源。創(chuàng)建三個個文件夾,分別為Dll、Schema、DBScript;分別用來存放NHibernate相關Dll文件、Schema文件和示例用到的數據庫腳本文件。下面需要把相應的文件拷貝到對應的文件夾下。【這一步可以做,也可以不做。一般在真實的項目都會做,方便統(tǒng)一管理】。

完成的NHibernate.Sample.Lib項目結構如下:

instnwnd.sql文件來自下, 載微軟Northwind示例數據庫。第一步已經提供了下載地址。Iesi.Collections.dll、NHibernate.dll、nhibernate-configuration.xsd、nhibernate-mapping.xsd四個文件都來自下載的NHibernate,Required_Bins文件目錄下。

6. NHibernate.Sample.Model項目,用來創(chuàng)建模型,對應于數據庫中的表。添加Customer類,添加Mapping文件夾。

  • 在Mapping文件夾下創(chuàng)建Customer.hbm.xml文件:

  • 創(chuàng)建好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; }    }}
  • 創(chuàng)建好以后NHibernate.Sample.Model項目的目錄結構如下:

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

  • 創(chuàng)建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();                retur
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 互助| 普陀区| 绵竹市| 长子县| 阿尔山市| 姜堰市| 石楼县| 汉寿县| 涿州市| 富平县| 宁海县| 利辛县| 通城县| 潮安县| 新龙县| 万山特区| 永顺县| 泸溪县| 沭阳县| 曲麻莱县| 浮山县| 甘孜县| 保定市| 什邡市| 孝义市| 石渠县| 麻栗坡县| 武平县| 沙坪坝区| 宁武县| 建宁县| 莆田市| 鱼台县| 彝良县| 古田县| 乌拉特后旗| 安阳县| 合水县| 东乡县| 巴东县| 湟源县|