1. 下载相关资源:

2. 下载NHibernate后解压缩文件,看到如下文档结构。本示例会用到Required_Bins目录下的文件。

  • 下载微软Northwind,打开SQL Server 直接运行instnwnd.sql文件的脚本就可以了。

3. 打开Visual Studio 2008。新建NHibernate.Sample解决方案。

4. 在新建的解决方案下创建如下新项目:

  • NHibernate.Sample.Business  【模板:类库】;处理业务逻辑
  • NHibernate.Sample.Data  【模板:类库】;处理数据访问
  • NHibernate.Sample.Lib  【模板:类库】;存放外部资源
  • NHibernate.Sample.Model  【模板:类库】;处理业务模型
  • NHibernate.Sample.Output  【模板:控制台应用程序】;测试输出

创建好以后,解决方案目录如下:

5. NHibernate.Sample.Lib项目,用来统一存放本示例用到的所有外部资源。创建三个个文件夹,分别为Dll、Schema、DBScript;分别用来存放NHibernate相关Dll文件、Schema文件和示例用到的数据库脚本文件。下面需要把相应的文件拷贝到对应的文件夹下。【这一步可以做,也可以不做。一般在真实的项目都会做,方便统一管理】。

完成的NHibernate.Sample.Lib项目结构如下:

instnwnd.sql文件来自下, 载微软Northwind示例数据库。第一步已经提供了下载地址。Iesi.Collections.dll、NHibernate.dll、nhibernate-configuration.xsd、nhibernate-mapping.xsd四个文件都来自下载的NHibernate,Required_Bins文件目录下。

6. NHibernate.Sample.Model项目,用来创建模型,对应于数据库中的表。添加Customer类,添加Mapping文件夹。

  • 在Mapping文件夹下创建Customer.hbm.xml文件:

  • 创建好Customer.hbm.xml文件以后,在打开的编辑界面内,右键=》属性。
  • 打开属性管理窗口:

  • 在属性管理窗口的架构一项,选择架构文件。架构文件的地址,就是我们上一步已经拷贝到NHibernate.Sample.Lib项目中的nhibernate-mapping.xsd文件。添加好以后编辑Customer.hbm.xml文件,就更够通过Visual Studio智能所用到的配置项。

  • 在解决方案管理器中找到Customer.hbm.xml文件,右键=》属性

  • 把“生成操作”属性改为:嵌入的资源。

  • 完成Customer.hbm.xml文件内容:【注意:assembly和namespace属性】
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Sample.Model" namespace="NHibernate.Sample.Model">
<class name="Customer" table="Customers" lazy="true">
<id name="CustomerID" column="CustomerID" type="string"/>
<property name="CompanyName" type="string">
<column name="CompanyName" length=""/>
</property>
<property name="ContactName" type="string">
<column name="ContactName" length=""/>
</property>
<property name="ContactTitle" type="string">
<column name="ContactTitle" length=""/>
</property>
<property name="Address" type="string">
<column name="Address" length=""/>
</property>
<property name="City" type="string">
<column name="City" length=""/>
</property>
<property name="Region" type="string">
<column name="Region" length=""/>
</property>
<property name="PostalCode" type="string">
<column name="PostalCode" length=""/>
</property>
<property name="Country" type="string">
<column name="Country" length=""/>
</property>
<property name="Phone" type="string">
<column name="Phone" length=""/>
</property>
<property name="Fax" type="string">
<column name="Fax" length=""/>
</property>
</class>
</hibernate-mapping>
  • 添加Customer.cs文件,完成Customer类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace NHibernate.Sample.Model
{
public class Customer
{
/// <summary>
///
/// </summary>
public virtual string CustomerID { get; set; } /// <summary>
///
/// </summary>
public virtual string CompanyName { get; set; } /// <summary>
///
/// </summary>
public virtual string ContactName { get; set; } /// <summary>
///
/// </summary>
public virtual string ContactTitle { get; set; } /// <summary>
///
/// </summary>
public virtual string Address { get; set; } /// <summary>
///
/// </summary>
public virtual string City { get; set; } /// <summary>
///
/// </summary>
public virtual string Region { get; set; } /// <summary>
///
/// </summary>
public virtual string PostalCode { get; set; } /// <summary>
///
/// </summary>
public virtual string Country { get; set; } /// <summary>
///
/// </summary>
public virtual string Phone { get; set; } /// <summary>
///
/// </summary>
public virtual string Fax { get; set; }
}
}
  • 创建好以后NHibernate.Sample.Model项目的目录结构如下:

7. NHibernate.Sample.Data项目,用来数据访问。创建文件夹Config,用来存放配置文件,创建数据访问基类,创建数据访问接口,创建数据访问类。

  • 创建NHiberane的配置文件hibernate.cfg.xml:

  • 设定hibernate.cfg.xml文件的框架,在文件的编辑窗口右键=》属性=》添加=》选择Schema文件(NHibernate.Sample.Lib项目中的nhibernate-configuration.xsd文件)。

  • 完成hibernate.cfg.xml文件的内容:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string_name">Connection String</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="show_sql">false</property>
<mapping assembly="NHibernate.Sample.Model"/>
</session-factory>
</hibernate-configuration>
  • 添加BaseOperator.cs文件,完成BaseOperator类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using NHibernate;
using NHibernate.Cfg; namespace NHibernate.Sample.Data
{
public class BaseOperator
{
private ISession m_Session; public ISession Session
{
get { return m_Session;}
} private ISessionFactory m_SessionFactory; public BaseOperator()
{
var config = new Configuration().Configure("Config/hibernate.cfg.xml");
m_SessionFactory = config.BuildSessionFactory();
m_Session = m_SessionFactory.OpenSession();
}
}
}
  • 添加CustomerOperator.cs文件,完成CustomerOperator类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using NHibernate.Linq;
using NHibernate.Sample.Model; namespace NHibernate.Sample.Data
{
public class CustomerOperator : BaseOperator
{ public object Save(Customer customer)
{
var id = Session.Save(customer);
Session.Flush();
return id;
} public void Update(Customer customer)
{
Session.Update(customer);
Session.Flush();
} public void Delete(Customer customer)
{
Session.Delete(customer);
Session.Flush();
} public Customer Get(object id)
{
return Session.Get<Customer>(id);
} public IList<Customer> GetAll()
{
return Session.Query<Customer>().ToList();
} }
}
  • 完成后的NHibernate.Sample.Data目录结构如下:

8. NHibernate.Sample.Business项目用来,处理业务逻辑;本示例比较简单,我们只添加一个逻辑处理类CustomerLogic,内容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using NHibernate.Sample.Data;
using NHibernate.Sample.Model; namespace NHibernate.Sample.Business
{
public class CustomerLogic
{
public IList<Customer> GetAll()
{
CustomerOperator oper = new CustomerOperator();
return oper.GetAll();
}
}
}
  • 完成以后项目结构如下:

9. NHibernate.Sample.Output为控制台项目,用来测试输出。添加一个配置文件App.config用于连接数据库,内容视个人环境而定。

App.config内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Connection String" connectionString="Data Source=.\SQLExpress;Initial Catalog=Northwind;Integrated Security=SSPI;" />
</connectionStrings>
</configuration>
  • Program文件内容如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Sample.Business;
using NHibernate.Sample.Model; namespace NHibernate.Sample.Output
{
class Program
{
static void Main(string[] args)
{
GetAll();
} private static void GetAll()
{
CustomerLogic logic = new CustomerLogic();
IList<Customer> cList = logic.GetAll(); foreach (Customer item in cList)
{
Console.WriteLine(item.CustomerID.ToString());
}
Console.Read();
}
}
}
  • NHibernate.Sample.Output为控制台项目下,E:\Study\NHibernate.Sample\NHibernate.Sample.Output\bin\Debug应包含如下文件。否则无法运行成功;如果缺少去其他项目中拷贝一份过来就可以了。

10. 运行结果如下:

11. 【代码下载

NHibernate实例的更多相关文章

  1. Nhibernate的第一个实例

    第一个NhIbernate程序 1.目的: a) 链接到oracle数据库 b) 增删改 c) 基本查询.sql查询 d) 视图查询 e) 使用存储过程 f) 多表查询.级联查询 g) 级联增删改 2 ...

  2. NHibernate简单使用介绍

    1.在数据库中新建表格,并插入记录,SQL如下: USE WFC_DB GO create table Students ( Id ,) not null, Name ), Age int, Scor ...

  3. NHibernate之旅系列文章导航

    NHibernate之旅系列文章导航 宣传语 NHibernate.NHibernate教程.NHibernate入门.NHibernate下载.NHibernate教程中文版.NHibernate实 ...

  4. NHibernate 基础教程

    NHibernate之旅系列文章导航 宣传语 NHibernate.NHibernate教程.NHibernate入门.NHibernate下载.NHibernate教程中文版.NHibernate实 ...

  5. NHibernate初学一之简单增删改查

    以前有简单了解NHibernate,但因项目一直没有运用所以也没有机会进行一个详细了解:最近利用一点空闲的时间认真学习它:同样希望把学习过程进行简单记录,今天简单写一个针对MSSQL数据库的增删改查的 ...

  6. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  7. 关于NHibernate中存在于Session中实例的3种状态的简单分析

    在使用NHibernate的时候.在Session中会有3种状态. 1. 瞬时状态 (Transient) 由 new 命令开辟内存空间的对象,也就是平时所熟悉的普通对象. 如: Student st ...

  8. NHibernate初学三之条件查询(Criteria Queries)与AspNetPager分页实例

    NHibernate除了SQL与HQL两种查询操作外,还有一种就是条件查询Criteria,本文将从网上整理一些Criteria的理论及小实例,最后通过一个结合AspNetPager分页来加深理解,必 ...

  9. NHibernate N+1问题实例分析和优化

    1.问题的缘起 考察下面的类结构定义 public class Category { string _id; Category _parent; IList<Category> _chil ...

随机推荐

  1. hdoj1075 What Are You Talking About

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  2. Python 2.7.9 Demo - 001.print_hello_world - 002.print_chinese

    001.print_hello_world #!/usr/bin/python print "hello, world..."; 002.print_chinese #coding ...

  3. How to Reuse Old PCs for Solr Search Platform?

    家裡或公司的舊電腦不夠力? 效能慢到想砸爛它們? 朋友或同事有電腦要丟嗎? 我有一個廢物利用的方法, 我收集了四台舊電腦, 組了一個Fully Distributed Mode的Hadoop Clus ...

  4. 快速写excel的方法

    对于用com组件写excel,笔者表示那个太慢了.而且很耗资源,还要装excel. 今天我们就用写文本文件的方式来写excel. 步骤1,用excel写好一个设计一个我们想要的模板. 步骤2,我们把做 ...

  5. echarts报错Cannot read property 'features' of undefined

    引入地图的时候 echarts2报错: Uncaught Error: [MODULE_MISS]"echarts/src/util/mapData/params" is not ...

  6. 安卓手机数据恢复软件-DiskDigger Pro

    以前的手机是支持大容量存储模式,可以被分配到盘符,但是自从手机不支持U盘大容量存储模式只能MTP模式之后,想要做数据恢复麻烦了很多啊! 经过多方查找,终于找到了这个能在手机上用的软件DiskDigge ...

  7. css3和jquery实现的可折叠导航菜单(适合手机网页)

    之前为大家介绍了好几款css3导航,今天为大家在介绍的是一款适合放在手机网页的导航菜单.点击列表图标以下拉式的形式显示菜单,单击关闭,动画关闭.效果相当不错.效果图如下: 在线预览   源码下载 这个 ...

  8. ucenter通信失败和不能登录的解决

    对于ucenter真是让人不省心,修改一下url,就通信失败了. 1.通信失败 然后后来怎么也改不好了,后来一步一步打log,发现是uc_server和uc_client不一致. 检查uc_serve ...

  9. php爬虫实践

    之前用nodejs的cheerio来做,不过nodejs的异步回掉太恶心了,受不了. 后来发现了php的htmlpagedom库,类似jquery的选择器语法,而且支持中文. 安装  composer ...

  10. java 支付宝wap支付初识

    最近突然想弄下支付宝的支付,因为感觉很好玩.中间遇到很多问题,查查找找,总算是整了两天给整出来了,这里为自己记录下. 第一步:直接去安卓支付宝的官方文档去,写的很清楚了已经,这里有源码https:// ...