![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
实体User
1
using System;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
namespace NHibernate.Examples.QuickStart
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
6
/// Summary description for User.
7
/// </summary>
8
public class User
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
private string id;
11
private string userName;
12
private string password;
13
private string emailAddress;
14
private DateTime lastLogon;
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public User()
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
}
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public string Id
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return id; }
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ id = value; }
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
public string UserName
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return userName; }
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ userName = value; }
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
public string Password
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return password; }
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ password = value; }
37
}
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
public string EmailAddress
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailAddress; }
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailAddress = value; }
43
}
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
public DateTime LastLogon
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return lastLogon; }
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ lastLogon = value; }
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
}
52
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
XML文件
1
<?xml version="1.0" encoding="utf-8" ?>
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
简单应用
1
using System;
2
using System.Collections;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
using NHibernate.Cfg;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
using NUnit.Framework;
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
namespace NHibernate.Examples.QuickStart
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
11
/// Summary description for UserFixture.
12
/// </summary>
13
[TestFixture]
14
public class UserFixture
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
[Test]
17
public void ValidateQuickStart()
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
Configuration cfg = new Configuration();
20
cfg.AddAssembly("NHibernate.Examples");
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
ISessionFactory factory = cfg.BuildSessionFactory();
23
ISession session = factory.OpenSession();
24
ITransaction transaction = session.BeginTransaction();
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
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 saved
34
session.Save(newUser);
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
// commit all of the changes to the DB and close the ISession
37
transaction.Commit();
38
session.Close();
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
// open another session to retrieve the just inserted user
41
session = factory.OpenSession();
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
User joeCool = (User)session.Load(typeof(User), "joe_cool");
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
// set Joe Cool's Last Login property
46
joeCool.LastLogon = DateTime.Now;
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
// flush the changes from the Session to the Database
49
session.Flush();
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
IList recentUsers = session.CreateCriteria(typeof(User))
52
.Add(Expression.Expression.Gt("LastLogon", new DateTime(2004, 03, 14, 20, 0, 0)))
53
.List();
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
foreach(User user in recentUsers)
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
Assert.IsTrue(user.LastLogon > (new DateTime(2004, 03, 14, 20, 0, 0)) );
58
}
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
session.Close();
61
}
62
}
63
}
文件里面有。没事放上来。回家看。
posted @
2007-03-30 17:22
南守拥
阅读(
264)
评论()
编辑
收藏
举报