第一步:在oracle中建立表CAT
ID varchar2(32)
NAME varchar2(50)
SEX char(1)
WEIGHT float
建立sequence CAT_SEQUENCE
第二步:建立web應用程式QuickStart,
添加相關dll
Iesi.Collections.dll
LinFu.DynamicProxy.dll
log4net.dll
NHibernate.ByteCode.Castle.dll
NHibernate.ByteCode.LinFu.dll
NHibernate.dll
nunit.framework.dll
添加 xsd
nhibernate-configuration.xsd
nhibernate-mapping.xsd
第三步:建立實體類
public class CAT { public CAT(){} public virtual string ID { get; set; } public virtual string NAME { get; set; } public virtual char SEX { get; set; } public virtual float WEIGHT { get; set; } }
第四步:添加對應檔 Cat.hbm.xml,修改屬性:【產生操作】【內嵌資源】
<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="QuickStart" namespace="QuickStart" > <class name="CAT" table="CAT"> <id name="ID" column="ID" type="string"> <!--<generator class="uuid.hex"/>--> <generator class="sequence"> <param name="sequence">CAT_SEQUENCE</param> </generator> </id> <property name="NAME"/> <property name="SEX"/> <property name="WEIGHT"/> </class></hibernate-mapping>
第五步:添加設定檔 hibernate.cfg.xml 【產生操作】【內嵌資源】【始終複製】
<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider </property> <property name="connection.driver_class"> NHibernate.Driver.OracleClientDriver </property> <property name="dialect"> NHibernate.Dialect.Oracle10gDialect </property> <property name="connection.connection_string"> User ID=EOLANDA;Password=EOLANDA;Data Source=150152 </property> <property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </property> <property name="show_sql">true</property> <property name="query.substitutions"> true 1, false 0, yes 'Y', no 'N' </property> <mapping assembly="QuickStart"/> </session-factory></hibernate-configuration>
第六步:寫一個類
public sealed class NHibernateHelper { private const string CurrentSessionKey = "nhibernate.current_session"; private static ISessionFactory sessionFactory; static NHibernateHelper() { sessionFactory = new Configuration().Configure().BuildSessionFactory(); } public static ISession GetCurrentSession() { HttpContext context = HttpContext.Current; ISession currentSession = context.Items[CurrentSessionKey] as ISession; if (currentSession == null) { currentSession = sessionFactory.OpenSession(); context.Items[CurrentSessionKey] = currentSession; } return currentSession; } public static void CloseSession() { HttpContext context = HttpContext.Current; ISession currentSession = context.Items[CurrentSessionKey] as ISession; if (currentSession == null) { return; } currentSession.Close(); context.Items.Remove(CurrentSessionKey); } public static void CloseSessionFactory() { if (sessionFactory != null) { sessionFactory.Close(); } } }
第七步:
public sealed class CatBal { public static void CreateCat(CAT cat) { ISession session = NHibernateHelper.GetCurrentSession(); ITransaction tx = session.BeginTransaction(); CAT princess = new CAT { NAME = cat.NAME, SEX = cat.SEX, WEIGHT = cat.WEIGHT, BIRTHDAY = cat.BIRTHDAY }; session.Save(princess); tx.Commit(); NHibernateHelper.CloseSession(); } }