Nhibernate学习笔记(1)
第一个NHibernate应用程序
环境
VS2005、NHibernate2.0、SQL Server2005
准备
下载NHibernate,建立数据库表
开始
1、创建C# Application项目Nhbnt
2、添加NHibernate.dll引用。由于NHibernate.dll引用了以下dll,所以同时需要这些的dll。
Castle.Core.dll
Castle.DynamicProxy2.dll
Iesi.Collections.dll
log4net.dll
3、添加Application Configuration File:App.config,内容如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<!-- Add this element -->
<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">
Data Source=DBtest02\DEV;Initial Catalog=PLForecast;User ID=PLForecast;Password=PLForecast
</property>
<!-- Need Changed -->
<mapping assembly="Nhbnt" />
</session-factory>
</hibernate-configuration>
</configuration>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<!-- Add this element -->
<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">
Data Source=DBtest02\DEV;Initial Catalog=PLForecast;User ID=PLForecast;Password=PLForecast
</property>
<!-- Need Changed -->
<mapping assembly="Nhbnt" />
</session-factory>
</hibernate-configuration>
</configuration>
4、编写持久化类(数据库对象类)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Person
{
private string id;
private string name;
private string description;
private string remark;
public virtual string Id { get { return id; } set { id = value; } }
public virtual string Name { get { return name; } set { name = value; } }
public virtual string Description { get { return description; } set { description = value; } }
public virtual string Remark { get { return remark; } set { remark = value; } }
}
{
private string id;
private string name;
private string description;
private string remark;
public virtual string Id { get { return id; } set { id = value; } }
public virtual string Name { get { return name; } set { name = value; } }
public virtual string Description { get { return description; } set { description = value; } }
public virtual string Remark { get { return remark; } set { remark = value; } }
}
5、编写持久化类与数据库的映射文件
我们要为Microsoft Visual Studio 2008添加编写NHibernate配置文件智能提示的功能。只要在下载的NHibernate里找到configuration.xsd和nhibernate-mapping.xsd两个文件并复制到X:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas目录即可。
新建一个XML文件,命名为Person.hbm.xml。
XML文件的默认生成操作为“内容”,这里需要修改为“嵌入的资源”
内容如下:
映射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Nhbnt" assembly="Nhbnt">
<class name="Person" table="T_Person">
<!-- A 32 hex character is our surrogate key. It's automatically generated by NHibernate with the UUID pattern. -->
<id name="Id">
<column name="PersonID" sql-type="varchar(50)" 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="PersonName" length="50" not-null="true" />
</property>
<property name="Description">
<column name="PersonDescription" length="50" not-null="false" />
</property>
<property name="Remark" />
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Nhbnt" assembly="Nhbnt">
<class name="Person" table="T_Person">
<!-- A 32 hex character is our surrogate key. It's automatically generated by NHibernate with the UUID pattern. -->
<id name="Id">
<column name="PersonID" sql-type="varchar(50)" 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="PersonName" length="50" not-null="true" />
</property>
<property name="Description">
<column name="PersonDescription" length="50" not-null="false" />
</property>
<property name="Remark" />
</class>
</hibernate-mapping>
6、编写持久化管理类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class SessionManager
{
private static readonly ISessionFactory sessionFactory;
static SessionManager()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public ISession GetSession()
{
return sessionFactory.OpenSession();
}
}
{
private static readonly ISessionFactory sessionFactory;
static SessionManager()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public ISession GetSession()
{
return sessionFactory.OpenSession();
}
}
7、现在可以使用ISession进行CRUD操作了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//Insert
ISession session = SessionManager.GetSession();
ITransaction tx = session.BeginTransaction();
Persion p = new Persion();
p.Name = "Persion";
p.Description = 'F';
session.Save(p);
tx.Commit();
session.Close();
//Select
ISession session = SessionManager.GetSession();
Persion p = session.Get<Persion>("id");
session.Close();
ISession session = SessionManager.GetSession();
ITransaction tx = session.BeginTransaction();
Persion p = new Persion();
p.Name = "Persion";
p.Description = 'F';
session.Save(p);
tx.Commit();
session.Close();
//Select
ISession session = SessionManager.GetSession();
Persion p = session.Get<Persion>("id");
session.Close();
结束:接下来我打算找一个持久化类的生成工具。这样将会减少很多工作。