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. MVC Nhibernate 示例

    首先,非常感谢提出问题的朋友们,使得本人又去深入研究了NHibernate的<Session-Per-Request 模式>.   前言: 谈到NHibernate大伙并不陌生,搞Java ...

  2. Linq to NHibernate入门示例

    Linq to NHibernate入门示例 NHibernate相关: 09-08-25连贯NHibernate正式发布1.0候选版 09-08-17NHibernate中一对一关联的延迟加载 09 ...

  3. NHibernate联合主键详细示例

    使用NHibernate实现一对多,多对一的关联很是简单,可如果要用复合主键实现确实让人有些淡淡的疼.虽然很淡疼但还是要去抹平这个坑,在下不才,愿意尝试. 以示例进入正文,源码下载地址: 一.数据表关 ...

  4. NHibernate查询示例合集

    基本查询   复杂查询示例 /// <summary> /// 获取自定义表单数据中属于部门的部分 /// </summary> /// <param name=&quo ...

  5. NHibernate使用简单示例

    NHibernate使用小示例 1.新建Model类库项目. 使用代码生成器生成Model类. 此处以简单的UserInfo表作为示例. 注意字段前必须以 virtual 修饰. namespace ...

  6. NHibernate 集合映射基础(第四篇) - 一对一、 一对多、多对多小示例

    映射文件,用于告诉NHibernate数据库里的表.列于.Net程序中的类的关系.因此映射文件的配置非常重要. 一.一对一 NHibernate一对一关系的配置方式使用<one-to-one&g ...

  7. NHibernate with ASP.NET MVC 入门示例

    目的:初步了解NHibernate的用法,包括数据库的CRUD, 基于ASP.NET MVC 项目模板 步骤: 创建ASP.NET MVC 新项目 使用NuGet引入FluentNHibernate ...

  8. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  9. 【翻译】首个基于NHibernate的应用程序

    首个基于NHibernate的应用程序  Your first NHibernate based application 英文原文地址:http://www.nhforge.org/wikis/how ...

随机推荐

  1. 驱动学习5: zynq实现点亮led

    驱动代码: #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #i ...

  2. ACM-ICPC2018 青岛赛区网络预赛-B- Red Black Tree

    题目描述 BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. A ...

  3. NOIP模拟赛8

    今天又爆零啦... T1 题目描述 #define goodcatdog gcd #define important i #define judge  j 神说 每个梦想就是一轮月亮,高高地孤寂地挂在 ...

  4. 【CodeForces】932 E. Team Work

    [题目]E. Team Work [题意]给定n和k,n个人中选择一个大小为x非空子集的代价是x^k,求所有非空子集的代价和%1e9+7.n<=10^9,k<=5000. [算法]斯特林反 ...

  5. 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql

    1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件  特地将执行map的个数设置为变量  测试 可以java代码传参数 ...

  6. 【洛谷 P3338】 [ZJOI2014]力(FFT)

    题目链接 \[\Huge{E_i=\sum_{j=1}^{i-1}\frac{q_j}{(i-j)^2}-\sum_{j=i+1}^{n}\frac{q_j}{(i-j)^2}}\] 设\(A[i]= ...

  7. 天梯赛 L2-012 关于堆的判断 (二叉树)

    将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: "x is the root":x是根结点: "x and y ar ...

  8. idea中tomcat乱码问题解决

    在idea中经常遇到jsp的乱码问题,原因是编码不是UTF-8的问题,这次来彻底解决idea的编码问题 首先设置idea编辑器的编码: File-Setting设置如下 然后配置tomcat的编码问题 ...

  9. python之yagmail库笔记

    1. yagmail是啥 yagmail是给正常人用的,封装的比较彻底的一个python邮件库,发送接收邮件只需要几行代码,炒鸡简单. 2. 安装 使用pip安装,炒鸡简单: pip install ...

  10. Mac 下安装 ruby 环境解决 brew 安装 yarn 问题

    在brew安装yarn提示 ruby的版本过低.在网上搜了一下发现 1. mac下自带的ruby 在 system 目录下 2. 其实可以用brew安装一个ruby brew install ruby ...