NHibernate教程1

一、NHibernate简介
 
在今日的企业环境中,把面向对象的软件和关系数据库一起使用可能是相当麻烦、浪费时间的。NHibernate是一个面向.Net环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping (ORM))这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。
 
NHibernate除了能将一张表映射为一个对象,还可以将表间关系变为对象的属性。例如学生和课程间的多对多关系就可以转化为学生类中的一个课程集合的属性。由此可见,NHibernate不仅仅管理.Net类到数据库表的映射,还提供数据查询和获取数据的方法。因此采用NHibernate,可以大幅度减少开发时人工使用SQL处理数据的时间。
 
 
 
二、NHibernate使用步骤
 
1.为系统引入NHibernate的库,并且配置NHibernate;
 
2.新建一个将要持久化.Net对象的表;
 
3.构建一个需要被持久化的.Net类;
 
4.构建一个可以让NHibernate知道如何完成ORM映射的XML文件;
 
5.使用NHibernate的API来编程

三、NHibernate的使用
 
1. 配置NHibernate
 
(1)NHibernate的配置文件有两种:在桌面应用程序(WinForm)中为App.config,在网页应用程序(WebForm)中为web.config。两种方法要添加的配置信息是一样的,目标都是告诉NHbiernate使用什么样的数据库,使用哪个库,用户密码分别是什么。
 
如果使用的是WinForm,将如下文本保存为App.config放到程序启动目录就可以。如果是WebForm,则将下面的文本添加到web.config中
 

 

 

<?xml version="1.0" encoding="utf-8" ?>
   <configuration>
      <configSections>
         <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </configSections>
      <nhibernate>
         <!--连接数据提供者 -->
         <add key="hibernate.connection.provider"
          value="NHibernate.Connection.DriverConnectionProvider" />
         <!--连接数据方言最常用的是MsSql2000Dialect -->
         <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
         <!--连接数据驱动类-->
         <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
         <!--数据库连接字符串-->
        <add key="hibernate.connection.connection_string" value="workstation id=BILLGATES;packet size=4096;integrated security=SSPI;initial catalog=CAW;persist security info=False" />
      </nhibernate>
   </configuration>
 


(2)然后将NHibernate唯一的一个dll(NHibernate.dll)添加到工程的引用中,步骤如下:
 
 à  à
 
2. 创建一个用于测试的表
 
这里需要创建3张表,分别用于保存:学生、课程、选课信息。可以用Sql语句来创建,也可以用其他方式创建。创建表的Sql语句如下:
 

 

 

CREATE TABLE Student (
 
 StudentId int identity (1, 1) PRIMARY KEY NOT NULL,

 StudentName nvarchar(40) default NULL,
 
 StudentPassword nvarchar(20) default NULL,
 
 EmailAddress nvarchar(40) default NULL,
 
 DateCreated datetime default NULL
 
)
 


CREATE TABLE Course (
 
 CourseId int identity (1, 1) PRIMARY KEY NOT NULL,

 CourseName nvarchar(40) default NULL,
 
 DateCreated datetime default NULL
 
)
 


CREATE TABLE StudentCourse (
 
 StudentCourseId int identity (1, 1) PRIMARY KEY NOT NULL,

 StudentId int,
 
 CourseId int,
 
 DateCreated datetime default NULL
 
)
 

 


产生的三张表的结构如下所示:

3. 创建.Net类和与其对应的XML文件
 
(1)       单表的情况
 
单表的情况就是指该表没有和其他表有关联的情况。这样情况下,可以用软件来辅助产生类和对应的XML文件。在这里,仍然采用手写的方式来生成这两个文件。对于课程表,需要写的类和XML如下所示:
 

 

 


类文件(Course.cs)
 
using System;
 
namespace NHibernateTest{
 
         public class Course{
 
                   public Course(){}
 
                   private int courseId;
 
                   private string courseName;
 
           private DateTime dateCreated;
 
                   public int CourseId{
 
                            get { return courseId; }
 
                            set { courseId = value; }
 
                   }
 
                   public string CourseName{
 
                            get { return courseName; }
 
                            set { courseName = value; }
 
                   }
 
                   public DateTime DateCreated{
 
                            get { return dateCreated; }
 
                            set { dateCreated = value; }
 
                   }                

         }
 
}
 

XML文件(Course.hbm.xml)
 
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
 
         <class name="NHibernateTest.Course, NHibernateTest" table="Course">
 
                   <id name="CourseId" column="CourseId" type="Int32">
 
                            <generator class="native" />
 
                   </id>
 
                   <property name="CourseName" column= "CourseName" type="string" length="40"/>                 

                   <property name="DateCreated" type="DateTime" length="8"/>
 
         </class>
 
</hibernate-mapping>
 


(2)       含有关系的表的情况
 
含有关系的表指的是像学生这样,除了保存学生的基本信息,还希望把选课信息保存到学生的类中。这样情况下不能用软件来辅助产生对应的类和XML,这是NHibernate中唯一需要费脑筋学的地方。学生表对应的类和XML如下所示:
 

 

 


类文件(Student.cs)
 
using System;
 
using Iesi.Collections;
 
namespace CodeTest{
 
         public class Student{
                   public Student(){}
 
                   private int studentId;
 
                   private string studentName;
 
                   private string studentPassword;
 
                   private string emailAddress;
 
                   private DateTime dateCreated;
 
                   private ISet courses =
 
 new HashedSet();
 
                   public int StudentId{
 
                            get { return studentId; }
 
                            set { studentId = value; }
 
                   }
 
                   public string StudentName{
 
                            get { return studentName; }
 
                            set { studentName = value; }         }
 
                   public string StudentPassword{
 
                            get { return studentPassword; }
 
                            set { studentPassword = value; }                   }
 
                   public string EmailAddress{
 
                            get { return emailAddress; }
 
                            set { emailAddress = value; }}
 
public DateTime DateCreated{
 
                            get { return dateCreated; }
 
                            set { dateCreated = value; }}
 
                   public ISet Courses{
 
                            get{return courses;}
 
                            set{courses=value;}
 
                   }                

         }
 
}
 

XML文件(Student.hbm.xml)
 
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
 
         <class name="NHibernateTest.Student, NHibernateTest" table="Student">
 
                   <id name="StudentId" column="StudentId" type="Int32">
 
                            <generator class="native" />
 
                   </id>
 
                   <property name="StudentName" column= "StudentName" type="string" length="40"/>

                   <property name="StudentPassword" type="string" length="20"/>

                   <property name="EmailAddress" type="String" length="40"/>

                   <property name="DateCreated" type="DateTime" length="8"/>
 
                  <set name="Courses" table="StudentCourse" inverse="false">
 
                   <key column="StudentID"/>
 
                   <many-to-many column="CourseID" class=" NHibernateTest.Course, NHibernateTest "/>
 
                   </set>
 
</class>
 
</hibernate-mapping>
 


4. 使用NHibernate的API进行编程
 
数据库编程不外乎“添加”、“删除”、“更新”,看看NHibernate是如何实现这三种操作吧。
 
(1)       在类中声明NHibernate的Factory
 
private static Configuration mCfg=new Configuration();
private static NHibernate.ISessionFactory SessionFactory=null;
 
(2)       在系统初始化的时候加载XML,并创建Factory
 
mCfg.AddXmlFile( ApplicationPath +"/Course.hbm.xml");
mCfg.AddXmlFile( ApplicationPath +"/Student.hbm.xml");
 
SessionFactory=mCfg.BuildSessionFactory();
 
(3)       使用HSql查询
 
string hsql="from Student where StudentName='kexd'";
 
ISession vSession= SessionFactory.OpenSession();
 
IQuery query = vSession.CreateQuery(hsql);

IList list = query.List();
 
if(list.Count>0){
 
   Student obj=(Student)list[0];
 
   //.....
 
}
 
vSession.Close();
 
(4)       根据主键查询
 
int studentID=1;
 
ISession vSession= SessionFactory.OpenSession();
 
Student obj=(Student) vSession.Load(typeof(Student),studentID);
 
vSession.Close();
 
(5)       插入
 
Student obj=new Student();
 
obj.StudentName="wuyang";
 
obj.StudentPassword="helloWorld";
 
ITransaction vTransaction = vSession.BeginTransaction();
 
try
 
{
 
   vSession.Save(obj);
 
   vTransaction.Commit();
 
}
 
catch(Exception)
 
{
 
   vTransaction.Rollback();
 
}
 
vSession.Close();
 
(6)       更新
 
ISession vSession= Config.SessionFactory.OpenSession();
 
IQuery query = vSession.CreateQuery("from Student where StudentName='kexd'");

IList list = query.List();

ITransaction vTransaction = vSession.BeginTransaction();
 
try
 
{
 
   foreach(Student obj in list)
 
   {
 
      obj.EmailAddress="kesfzu@21cn.com";
 
      vSession.Save(obj);

   }
 
   vTransaction.Commit();
 
}
 
catch(Exception)
 
{
 
   vTransaction.Rollback();
 
}
 
finally
 
{
 
   vSession.Close();
 
}
 
(7)       删除
 
ISession vSession= Config.SessionFactory.OpenSession();
 
IQuery query = vSession.CreateQuery("from Student where StudentName='kexd'");

IList list = query.List();

ITransaction vTransaction = vSession.BeginTransaction();
 
try
 
{
 
   foreach(Student obj in list)
 
   {
 
           vSession.Delete(obj);
 
   }
 
   vTransaction.Commit();
 
}
 
catch(Exception)
 
{
 
   vTransaction.Rollback();
 
}
 
finally
 
{
 
   vSession.Close();
 
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/su317/archive/2009/08/06/4413869.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值