实体User 1using System; 2 3namespace NHibernate.Examples.QuickStart 4{ 5 /**//// <summary> 6 /// Summary description for User. 7 /// </summary> 8 public class User 9 {10 private string id;11 private string userName;12 private string password;13 private string emailAddress;14 private DateTime lastLogon;151617 public User()18 {19 }2021 public string Id 22 {23 get { return id; }24 set { id = value; }25 }2627 public string UserName 28 {29 get { return userName; }30 set { userName = value; }31 }3233 public string Password 34 {35 get { return password; }36 set { password = value; }37 }3839 public string EmailAddress 40 {41 get { return emailAddress; }42 set { emailAddress = value; }43 }4445 public DateTime LastLogon 46 {47 get { return lastLogon; }48 set { lastLogon = value; }49 }5051 }52} XML文件 1<?xml version="1.0" encoding="utf-8" ?> 2 3<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 4 5 <class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples" table="users" 6 lazy="false"> 7 <id name="Id" column="LogonId" type="String" length="20"> 8 <generator class="assigned" /> 9 </id>10 11 <property name="UserName" column="Name" type="String" length="40"/>12 <property name="Password" type="String" length="20"/>13 <property name="EmailAddress" type="String" length="40"/>14 <property name="LastLogon" type="DateTime"/>15 </class>16 17</hibernate-mapping> 简单应用 1using System; 2using System.Collections; 3 4using NHibernate.Cfg; 5 6using NUnit.Framework; 7 8namespace NHibernate.Examples.QuickStart 9{10 /**//// <summary>11 /// Summary description for UserFixture.12 /// </summary>13 [TestFixture]14 public class UserFixture15 {16 [Test]17 public void ValidateQuickStart() 18 {19 Configuration cfg = new Configuration();20 cfg.AddAssembly("NHibernate.Examples");2122 ISessionFactory factory = cfg.BuildSessionFactory();23 ISession session = factory.OpenSession();24 ITransaction transaction = session.BeginTransaction();2526 User newUser = new User();27 newUser.Id = "joe_cool";28 newUser.UserName = "Joseph Cool";29 newUser.Password = "abc123";30 newUser.EmailAddress = "joe@cool.com";31 newUser.LastLogon = DateTime.Now;32 33 // Tell NHibernate that this object should be saved34 session.Save(newUser);3536 // commit all of the changes to the DB and close the ISession37 transaction.Commit();38 session.Close();3940 // open another session to retrieve the just inserted user41 session = factory.OpenSession();4243 User joeCool = (User)session.Load(typeof(User), "joe_cool");4445 // set Joe Cool's Last Login property46 joeCool.LastLogon = DateTime.Now;4748 // flush the changes from the Session to the Database49 session.Flush();5051 IList recentUsers = session.CreateCriteria(typeof(User))52 .Add(Expression.Expression.Gt("LastLogon", new DateTime(2004, 03, 14, 20, 0, 0)))53 .List();5455 foreach(User user in recentUsers)56 {57 Assert.IsTrue(user.LastLogon > (new DateTime(2004, 03, 14, 20, 0, 0)) ); 58 }5960 session.Close();61 }62 }63} 文件里面有。没事放上来。回家看。
转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/03/30/694266.html
