只有在实际运用中才能真正的遇到问题 ,并在解决问题的过程中不断提高
在网上参考了 http://blog.aspcool.com/tim/posts/1133.aspx
http://nhibernate.3yee.com/archive/2004/04/26/439.aspx
等多篇文章后
所以 打算写基于NHibernate 的一个小小的留言本,
首先 建数据库
CREATE
TABLE
[
dbo
]
.
[
G_guestbook
]
(
[
id
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
userid
]
[
int
]
NOT
NULL
,
[
username
]
[
nvarchar
]
(
20
) COLLATE Chinese_PRC_CI_AS
NOT
NULL
,
[
title
]
[
nvarchar
]
(
100
) COLLATE Chinese_PRC_CI_AS
NOT
NULL
,
[
article
]
[
ntext
]
COLLATE Chinese_PRC_CI_AS
NULL
,
[
pubtime
]
[
datetime
]
NOT
NULL
)
ON
[
PRIMARY
]
TEXTIMAGE_ON
[
PRIMARY
]
GO
![None.gif](/Images/OutliningIndicators/None.gif)
CREATE
TABLE
[
dbo
]
.
[
G_users
]
(
[
id
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
Name
]
[
nvarchar
]
(
20
) COLLATE Chinese_PRC_CI_AS
NOT
NULL
,
[
email
]
[
nvarchar
]
(
40
) COLLATE Chinese_PRC_CI_AS
NULL
,
[
password
]
[
nvarchar
]
(
20
) COLLATE Chinese_PRC_CI_AS
NOT
NULL
,
[
flag
]
[
int
]
NOT
NULL
,
[
regtime
]
[
datetime
]
NULL
)
很简单的结构,可以用就行了,
第二步是打开 VS.net 新建一个
guestbook.data的工程 ,在这里不得不感谢飞鹰,提供了cool coder这个好东东啊, 虽然还不是哪么的完美,但是生成的实体类,各 映射文件只要稍加改动就可以加到 我的工程里了,节省了大量的时间啊,
我的两个实体类以及 映射文件
using
System;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
guestbook.data
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class guestbooks
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public guestbooks()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.String _article;
public System.String article
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _article; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _article = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.Int32 _id;
public System.Int32 id
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _id = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.DateTime _pubtime;
public System.DateTime pubtime
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _pubtime; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _pubtime = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.String _title;
public System.String title
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _title; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _title = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.Int32 _userid;
public System.Int32 userid
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _userid; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _userid = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.String _username;
public System.String username
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _username; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _username = value; }
}
}
}
using
System;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
guestbook.data
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class users
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public users()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.Int32 _flag;
public System.Int32 flag
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _flag; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _flag = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.String _email;
public System.String email
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _email; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _email = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.Int32 _id;
public System.Int32 id
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _id = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.DateTime _regtime;
public System.DateTime regtime
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _regtime; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _regtime = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.String _Name;
public System.String Name
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Name; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _Name = value; }
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private System.String _password;
public System.String password
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _password; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _password = value; }
}
}
}
还有两个映射文件
<?
xml version="1.0" encoding="utf-8"
?>
<
hibernate-mapping
xmlns
="urn:nhibernate-mapping-2.0"
>
<
class
name
="guestbook.data.users, guestbook.data"
table
="G_users"
>
<
id
name
="id"
column
="id"
type
="Int32"
>
<
generator
class
="identity"
/>
</
id
>
<
property
name
="Name"
type
="String(20)"
column
="Name"
/>
<
property
name
="email"
type
="String(40)"
column
="email"
/>
<
property
name
="password"
type
="String(20)"
column
="password"
/>
<
property
name
="flag"
type
="Int32"
column
="flag"
/>
<
property
name
="regtime"
type
="DateTime"
column
="regtime"
/>
</
class
>
</
hibernate-mapping
>
![None.gif](/Images/OutliningIndicators/None.gif)
<?
xml version="1.0" encoding="utf-8"
?>
<
hibernate-mapping
xmlns
="urn:nhibernate-mapping-2.0"
>
<
class
name
="guestbook.data.guestbooks, guestbook.data"
table
="G_guestbook"
>
<
id
name
="id"
column
="id"
type
="Int32"
>
<
generator
class
="identity"
/>
</
id
>
<
property
name
="userid"
type
="Int32"
column
="userid"
/>
<
property
name
="username"
type
="String(20)"
column
="username"
/>
<
property
name
="title"
type
="String(100)"
column
="title"
/>
<
property
name
="article"
type
="String"
column
="article"
/>
<
property
name
="pubtime"
type
="DateTime"
column
="pubtime"
/>
</
class
>
</
hibernate-mapping
>
由于我用的是自动编号,所以,要将
<generator class="assigned" />改为 <generator class="identity"" />
为了能更早的了解代码是否正确,免除只能把界面搭建出来以后才能测试一个小模块的代码的尴尬,所以 请出了,NUnit 虽然我还不是怎么会用,但是一个能你让还不怎么对它怎么熟悉,但就觉得非常好用的软件,实在是太强了
添一个新建项目 命名为 guestbook.test
写一个测试文件
这里的主要目地是为了验证 配置的 正确与否,所以测试文件也写的简单一些了,
using
System;
using
System.Collections;
using
NHibernate;
using
NHibernate.Cfg;
![None.gif](/Images/OutliningIndicators/None.gif)
using
NUnit.Framework;
![None.gif](/Images/OutliningIndicators/None.gif)
using
guestbook.data;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
guestbook.test
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// DataLayerUsersTest 的摘要说明。
/// </summary>
[TestFixture]
public class DataLayerUsersTest
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public DataLayerUsersTest()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
[Test]public void testAdd()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Configuration cfg=new Configuration();
cfg.AddXmlFile("users.hbm.xml");
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
ISessionFactory f=cfg.BuildSessionFactory();
ISession s=f.OpenSession();
ITransaction t=s.BeginTransaction();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
users newUser=new users();
newUser.Name="papersnake";
newUser.password="24976904";
newUser.email="papersnakes@gmail.com";
newUser.regtime=DateTime.Now;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
s.Save(newUser);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
t.Commit();
s.Close();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
}
}
另一个文件也大同小意不放出来了,编译
打开Nunit ,run 一下,出现一道美丽的绿色。
心情不是一班的好啊,哇卡卡