在看了NHibernate 2.0的文档之后,根据文档的操作步骤做的第一个实例,刚开始的时候做的好久没懂出来,为了开始学习NHibernate的初学者,将的第一个NHibernate实例详细过程的说明一下:
实例环境:
1.Microsoft Visual Studio 2005
2.Microsoft SQL Server 2005
3.NHibernate 2.0
4.NUnit 2.0
以下是我做的过程:
1.创建数据库表
2.配置Web.config文件
3.编写实体类
4.编写映射文件
5.编写CRUD操作,2832785553
1.创建数据库表
Code
CREATE TABLE [dbo].[Cat](
[CatId] [char](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Sex] [nchar](1) COLLATE Chinese_PRC_CI_AS NULL,
[Weight] [real] NULL,
CONSTRAINT [PK_Cat] PRIMARY KEY CLUSTERED
(
[CatId] ASC
)
)
CREATE TABLE [dbo].[Cat](
[CatId] [char](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Sex] [nchar](1) COLLATE Chinese_PRC_CI_AS NULL,
[Weight] [real] NULL,
CONSTRAINT [PK_Cat] PRIMARY KEY CLUSTERED
(
[CatId] ASC
)
)
2.配置Web.config文件
Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- 添加一个元素-->
<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
</configSections>
<!-- 添加元素-->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">
Server=(local);initial catalog=quickstart;Integrated Security=SSPI
</property>
<mapping assembly="QuickStart" />
</session-factory>
</hibernate-configuration>
<!-- Leave the system.web section unchanged -->
<system.web>
</system.web>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- 添加一个元素-->
<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
</configSections>
<!-- 添加元素-->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">
Server=(local);initial catalog=quickstart;Integrated Security=SSPI
</property>
<mapping assembly="QuickStart" />
</session-factory>
</hibernate-configuration>
<!-- Leave the system.web section unchanged -->
<system.web>
</system.web>
</configuration>
3.编写实体类
Code
public class Cat
{
private string id;
private string name;
private char sex;
private float weight;
public Cat()
{
}
public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual char Sex
{
get { return sex; }
set { sex = value; }
}
public virtual float Weight
{
get { return weight; }
set { weight = value; }
}
}
public class Cat
{
private string id;
private string name;
private char sex;
private float weight;
public Cat()
{
}
public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual char Sex
{
get { return sex; }
set { sex = value; }
}
public virtual float Weight
{
get { return weight; }
set { weight = value; }
}
}
4.编写映射文件
Code
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="QuickStart" assembly="QuickStart">
<class name="Cat" table="Cat">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
<id name="Id">
<column name="CatId" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="Name">
<column name="Name" length="16" not-null="true" />
</property>
<property name="Sex" />
<property name="Weight" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="QuickStart" assembly="QuickStart">
<class name="Cat" table="Cat">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
<id name="Id">
<column name="CatId" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="Name">
<column name="Name" length="16" not-null="true" />
</property>
<property name="Sex" />
<property name="Weight" />
</class>
</hibernate-mapping>
5.Web项目中调用,