Nhibernate3.3.3 GA使用初探
首先,下載Nhibernate3.3.3 GA(http://sourceforge.net/projects/nhibernate/)
拿到bins下的文件:
新建項目,結(jié)構(gòu)如下:
說明:
NHibernate.DataPortal是數(shù)據(jù)門戶,
NHibernate.Domain是Model層,
NHibernateUI是界面層,典型的三層架構(gòu)
?
對相關(guān)文件的解釋:
NHibernate.Domain中,Entities文件夾內(nèi)的Customer.cs
View Code using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace NHibernate.Domain.Entities {[Serializable]public class Customer {#region Customerpublic virtual string CustomerID { get; set; }public virtual string CompanyName { get; set; }public virtual string ContactName { get; set; }public virtual string ContactTitle { get; set; }public virtual string Address { get; set; }public virtual string City { get; set; }public virtual string Region { get; set; }public virtual string PostalCode { get; set; }public virtual string Country { get; set; }public virtual string Phone { get; set; }public virtual string Fax { get; set; }#endregion} }?針對Customer的配置文件Customer.hbm.xml,同時把文件的屬性的默認(rèn)生成操作為“內(nèi)容”,這里需要修改為“嵌入的資源”生成,因為NHibernate是通過查找程序集中的資源文件映射實體
View Code <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Domain" namespace="NHibernate.Domain.Entities"><class name="NHibernate.Domain.Entities.Customer,NHibernate.Domain" table="Customers"><id name="CustomerID" column="CustomerID" type="string" unsaved-value="0"><generator class="increment"/></id><property name="CompanyName" column="CompanyName" type="string" /><property name="ContactName" column="ContactName" type="string" /><property name="ContactTitle" column="ContactTitle" type="string" /><property name="Address" column="Address" type="string" /><property name="City" column="City" type="string" /><property name="Region" column="Region" type="string" /><property name="PostalCode" column="PostalCode" type="string" /><property name="Country" column="Country" type="string" /><property name="Phone" column="Phone" type="string" /><property name="Fax" column="Fax" type="string" /></class> </hibernate-mapping>NHibernate.DataPortal中NHibernateHelper類代碼:
View Code using NHibernate.Cfg;namespace NHibernate.DataPortal {public class NHibernateHelper {private ISessionFactory _sessionFactory;public NHibernateHelper() {_sessionFactory = GetSessionFactory();}private ISessionFactory GetSessionFactory() {return (new Configuration()).Configure().BuildSessionFactory();}public ISession GetSession() {return _sessionFactory.OpenSession();}} }NHibernate.DataPortal中CustomerDal類代碼
View Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using NHibernate.Domain.Entities;namespace NHibernate.DataPortal {public class CustomerDal {private NHibernateHelper nhibernateHelper = new NHibernateHelper();protected ISession Session { get; set; }public CustomerDal() {this.Session = nhibernateHelper.GetSession();}public CustomerDal(ISession session) {this.Session = session;}public void CreateCustomer(Customer customer) {Session.Save(customer);Session.Flush();}public Customer GetCustomerById(int customerId) {return Session.Get<Customer>(customerId);}public IList<Customer> GetCunstomers() {IList<Customer> list = null;list = Session.QueryOver<Customer>().List();return list;}} }關(guān)鍵點,與數(shù)據(jù)庫打交道的配置文件hibernate.cfg.xml,把文件的默認(rèn)“復(fù)制到輸出目錄”為“不復(fù)制”,這里需要修改為“始終復(fù)制”
View Code <?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"><session-factory><property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property><property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property><property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property><property name="connection.connection_string">Server=.;initial catalog=northwind;Integrated Security=True</property><property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property> <property name="show_sql">true</property><property name="command_timeout">10</property><property name="adonet.batch_size">10</property><property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property><mapping assembly="NHibernate.Domain" /></session-factory> </hibernate-configuration>NHibernateUI層窗體的調(diào)用
View Code protected override void OnLoad(EventArgs e) {try {NHibernate.DataPortal.CustomerDal customerDal = new NHibernate.DataPortal.CustomerDal();this.dgvCustomerList.DataSource = customerDal.GetCunstomers();} catch (Exception ex) {throw new Exception(ex.Message);}}最后顯示結(jié)果
在這次使用過程中,發(fā)生了很多的問題,但是因為NHibernate 3.3.3版本剛出來不久,使用的案例比較少,特別是很多配置的地方特別不一樣,所以費(fèi)了很多的周折,寫給自己備用,也希望給朋友你帶來一些幫助
轉(zhuǎn)載于:https://www.cnblogs.com/cpine/archive/2013/05/04/NHibernate%e9%85%8d%e7%bd%ae.html
總結(jié)
以上是生活随笔為你收集整理的Nhibernate3.3.3 GA使用初探的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Enterprise Solution
- 下一篇: C++ 接收数量不定的函数参数