惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
The Cloudflare Blog
V
Visual Studio Blog
G
Google Developers Blog
博客园 - Franky
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
AWS News Blog
AWS News Blog
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hacker News: Front Page
博客园 - 聂微东
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
A
About on SuperTechFans
V2EX - 技术
V2EX - 技术
腾讯CDC
Project Zero
Project Zero
T
Tor Project blog
C
CERT Recently Published Vulnerability Notes
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Attack and Defense Labs
Attack and Defense Labs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
Security Latest
Security Latest
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Scott Helme
Scott Helme
Microsoft Security Blog
Microsoft Security Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
P
Proofpoint News Feed
罗磊的独立博客
M
MIT News - Artificial intelligence
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
量子位
博客园 - 司徒正美
U
Unit 42
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 谭洪星

MVC模式在.NET框架中的应用与实现 混乱的MVC,.NET非要MVC不可么?(转) ASP.NET实现URL映射 PetShop的系统架构设计(转) ASP.NET中常用的优化性能方法 设计模式学习笔记(三)——Abstract Factory抽象工厂模式 (转) 观察者模式及实现(转) 悟透JavaScript (转) 深入研究Asp.net页面的生命周期 [你必须知道的.NET] 第一回:恩怨情仇:is和as Net中的反射使用入门 .Net 中的反射(反射特性) - Part.3 .net反射简介 软件工程心理学之---让客户知错,但不能向你发怒 用AJAX.NET的客户端脚本实现UpdateProgress的效果 - 谭洪星 - 博客园 C#(也适用其他)的初学者对string是值类型还是引用类型搞不清楚,还有对参数传递也比较迷糊 C#泛型之详解 [你必须知道的.NET]第十二回:参数之惑---传递的艺术(下) [你必须知道的.NET]第十一回:参数之惑---传递的艺术(上)
NHibernate教程(转载)
谭洪星 · 2010-06-05 · via 博客园 - 谭洪星

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.使用NHibernateAPI来编程

三、NHibernate的使用

1. 配置NHibernate

1NHibernate配置文件有两种:在桌面应用程序(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唯一的一个dllNHibernate.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. 使用NHibernateAPI进行编程

数据库编程不外乎“添加”、“删除”、“更新”,看看NHibernate是如何实现这三种操作吧。

(1)       在类中声明NHibernateFactory

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();

}

转的多了,自然也就会了.