Nhibernate 2.0简单范例
时间:2011-03-23 来源:eolande
NHibernate官网:http://nhforge.org/
官网只有最新版本,找不到hibernate2.0
下载这里:NHibernate-2.1.2.GA-bin.zip
解压缩后,在里面找这些dll,加入项目参考:
Castle.Core.dll
Castle.DynamicProxy2.dll
Iesi.Colections.dll
Nhibernate.dll
Nhibernate.ByteCode.Castle.dll
先引用这些参考,其它暂时用不到。
以下的例子是NHibernate中文文档.rar中的,因为写的不全,做出来的例子运行成功,查看其它资料做好的完整例子步骤:
1.新建数据库quickstart.(sql2008) ,CatId(char(32)),Name(nvarchar(16)),Sex(nchar(1)),Weight(real)
2.vs2010新建web应用程序QuickStart,新建类Cat.cs:
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; } }
3.新建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 name="CatId" sql-type="char(32)" not-null="true"/> <generator class="uuid.hex"/> </id> <property name="Name"> <column name="Name" length="16" not-null="true"/> </property> <property name="Sex"/> <property name="Weight"/> </class> </hibernate-mapping>
4.新建hibernate.cfg.xml文件并写入以下内容:
(说明:链接的是sql2008数据库,数据库名字是quickstart。)
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Data Source=.\sqlexpress;Initial Catalog=quickstart;Persist Security Info=True;User ID=sa;Password=sa;Pooling=False</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="use_outer_join">true</property> <property name="command_timeout">10</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="adonet.batch_size">10</property> <property name="show_sql">true</property> <mapping assembly="QuickStart" /> </session-factory> </hibernate-configuration>
5.新建NHibernateHelper.cs
using System; using System.Web; using NHibernate; using NHibernate.Cfg; namespace QuickStart { public sealed class NHibernateHelper { private const string CurrentSessionKey = "nhibernate.current_session"; private static ISessionFactory sessionFactory; static NHibernateHelper() { ISessionFactory factory = new Configuration().Configure().BuildSessionFactory(); 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(); } } } }
6.编辑Default.aspx
Name:<asp:TextBox ID="txtName" Text="" runat="server" /> Sex:<asp:TextBox ID="txtSex" Text="" runat="server" /> Weight:<asp:TextBox ID="txtWeight" Text="" runat="server" /> <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
7.编辑Default.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e) { CatBal.CreateCat(txtName.Text, txtSex.Text, txtWeight.Text); }
完。
相关阅读 更多 +