兄弟也是在慢慢学习NHibernate下面所说的NHibernate也是我个人对NHibernate的理解,我讲解的NHibernate主要是用在实际项目开发中的,其中有什么理解不正确的请各路高手指正,兄弟在这谢过了。

     NHibernate是一个面向.NET环境的对象/关系数据库映射工具。对象/关系数据库映射(ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。

   NHibernate不仅仅管理.NET类到数据库表的映射,还提供数据查询和获取数据的方法,可以大幅度减少开发时人工使用SQL和ADO.NET处理数据的时间。

首先我们要下载NHibernate最新版本 http://sourceforge.net/projects/nhibernate/ s

配置NHibernate

代码
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace NHibernateStudy
{
class Program
{
static void Main(string[] args)
{
//第一步:获取Program类所在的程序集
Assembly assembly = typeof(Program).Assembly;
//第二部配置
Configuration configuration = new Configuration()
//读取hibernate.cfg.xml 文件
.Configure()
// 加载 类 和对应的xml
.AddAssembly(assembly)
// 处理标记
.AddInputStream(HbmSerializer.Default.Serialize(assembly));
//建造会话工厂,以便建立会话
ISessionFactory sessionFactory = configuration.BuildSessionFactory();
ISession session
= sessionFactory.OpenSession();

}
}
}

配置数据库连接信息

代码
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="current_session_context_class">thread_static</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Spring.ProxyFactoryFactory, NHibernate.ByteCode.Spring</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="show_sql">true</property>
<property name="connection.connection_string">
Data Source
=cml;User Id = sa;Password=sql;Initial Catalog=Northwind;
</property>
<property name="adonet.batch_size">500</property>
<property name="generate_statistics">true</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
</session-factory>
</hibernate-configuration>

这个数据库配置信息可以说是一个固定模式,如果需要我们就改数据库版本,实际在网上都能找到<propertyname="dialect">NHibernate.Dialect.MsSql2005Dialect</property>

连接数据库的这段改成自己的数据库相应连接就可以

<property name="connection.connection_string">
      Data Source=cml;User Id = sa;Password=sql;Initial Catalog=Northwind;
    </property>

持久化类:

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using NHibernate.Mapping.Attributes;
6
7  namespace NHibernateStudy
8 {
9 [Class(Table="Orders", NameType=typeof(Order))]
10 public class Order: DomainModel
11 {
12 [Id(Name = "ID", Column = "OrderID")]
13 [Generator(1, Class = "native")]
14 virtual public int ID { get; set; }
15
16 [Property]
17 virtual public DateTime? OrderDate { get; set; }
18
19
20 [Property]
21 virtual public decimal? Freight { get; set; }
22
23 [Property]
24
25 virtual public int EmployeeID { get; set; }
26
27 [ManyToOne(Column = "CustomerID")]
28 virtual public Customer Customer { get; set; }
29
30 [ManyToOne(Column = "EmployeeID", Fetch = FetchMode.Select)]
31 virtual public Employee Employee { get; set; }
32
33
34 public override string ToString()
35 {
36 return ID.ToString();
37 }
38 }
39 }
40  

  Hibernate使用简单的.Net对象(POCOs)这种编程模型来进行持久化,POCO类通过set和get属性 访问其内部成员变量,对外则隐藏了内部实现的细节看实例。

    NHibernate对属性使用的类型不加任何限制。所有的.NET类型和原始类型(比如string,char和DateTime)都可以被映射,也包括.Net 集合(System.Collections)中的类。你可以把它们映射成为值,值集合,或者与其他实体类相关联。Id是一个特殊的属性,代表了这个类的数据库标识符(主键)。

 为了让上面提到运行时类增强功能生效,NHibernate持久化类的所有的public的属性必须声明为virtual。

映射Employee

代码
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="NHibernateStudy.Employee, NHibernateStudy" table="Employees">
<id name="ID" column="EmployeeID">
<generator class="native" />
</id>
<property name="FirstName" column="FirstName"/>
<property name="LastName" column="LastName"/>
<property name="Title" column="Title"/>
</class>
</hibernate-mapping>

Employee.hbm.xml映射文件包含了对象/关系映射所需的元数据。元数据包含持久化类的声明和属性到数据库的映射(指向字段和其他实体的外键关联)。

   我们现在可以开始使用NHibernate的ISession了。它是一个 持久化管理器, 我们通过ISession来从数据库中存取数据。

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using NHibernate.Mapping.Attributes;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace NHibernateStudy
{
class Program
{
static void Main(string[] args)
{
Assembly assembly
= typeof(Program).Assembly;

Configuration configuration
= new Configuration()
.Configure()
.AddAssembly(assembly)
.AddInputStream(HbmSerializer.Default.Serialize(assembly));

ISessionFactory sessionFactory
= configuration.BuildSessionFactory();
ISession session
= sessionFactory.OpenSession();

//NHibernate有不同的方法用来从数据库中取回对象

// 查询编号为 9 的员工09年 所作订单的运费总和

var v2
= session.Linq<Order>()
.Where(o
=> o.Employee.ID == 9)
.Where(o
=> o.OrderDate >= new DateTime(1996, 1, 1) && o.OrderDate < new DateTime(1997, 1, 1))
.Sum(o
=> o.Freight);




}
}
}


这就是我对NHibernate的理解,如有那理解错误请给指出。

注:Employee.hbm.xml映射文件以后不要用太麻烦了,可以在NHibernate配置中配置自动生成XML文件,强烈反对用生成器生成NHibernate(个人反对)。

源码下载地址: https://files.cnblogs.com/changminglong/NHibernate.rar

posted on 2010-08-27 17:35  异样的世界  阅读(4644)  评论(5编辑  收藏  举报