NHibernate简介:

  NHibernate是一个面向.net环境的对象/关系数据库映射工具,对象/关系数据库映射(object/relational mapping,ORM)是一种技术,可以将对象模型表示的对象映射到基于SQL的关系型数据结构中去。NHibernate是一个基于.net的针对关系型数据的持久化类库。NHibernate是主要用于数据持久化编程。

1.新建MVC项目

2.项目架构:

采用传统三层架构:

  • Domain:领域层,存放实体和映射文件
  • Data:数据层,存放数据库的操作已经NHibernate辅助类,引用Iesi.Collections.dll,NHibernate.dll和类库领域层类
  • Business:业务逻辑层,用来处理实体和数据。得引用领域层类(实体)和数据层类(数据操作)。
  • website:实际应用的地方,要引用领域层(实体)和业务逻辑层(对实体进行业务逻辑的操作)

3.在数据层安装NHibernate,可以使用VS自带nuget来安装  Install-Package Nhibernate ,安装完后就会有相应的引用

4.在项目文件的目录的packages下能找到NHibernate的数据库配置模版。找到后添加到我们要实际操作的项目的根目录去,进行相应的配置。注意将cfg.xml文件的属性改成始终复制。

<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<!--定制数据库IDriver的类型.-->
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<!--连接字符串-->
<property name="connection.connection_string">
Server=.;database=Northwind;uid=sa;pwd=yujie1127
</property>
<!--NHibernate方言(Dialect)的类名 - 可以让NHibernate使用某些特定的数据库平台的特性-->
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<!--指定映射文档中所在程序集-->
<mapping assembly="Shop.Domain"/>
</session-factory>
</hibernate-configuration>

5.编写辅助类

  在数据层编写辅助类,用于创建用于创建ISessionFactory和配置ISessionFactory,并打开一个新的ISession的方法。在Data项目下新建一个类NHibernateHelper:

第一步:先申明一个IsessionFactory,第二步:创建IsessionFactory,第三步:配置IsessionFactory,第四步:开始Isession。

using NHibernate;
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Shop.Data
{
public class NHibernateHelper
{  //先申明一个ISessionDFactory()会话工厂
private ISessionFactory _sessionFactory;
public NHibernateHelper()
{
//创建ISessionFactory
_sessionFactory = GetSessionFactory();
} /// <summary>
/// 创建ISessionFactory
/// </summary>
/// <returns></returns>
public ISessionFactory GetSessionFactory()
{
//配置ISessionFactory
return (new Configuration()).Configure().BuildSessionFactory();
} /// <summary>
/// 打开ISession
/// </summary>
/// <returns></returns>
public ISession GetSession()
{
return _sessionFactory.OpenSession();
}
}
}

6.持久化类:

为客户实体创建持久化类Customers。在项目Shop.Domain中新建文件夹Entities,然后新建类Customers,这里为了偷懒,我就使用动软工具生成代码如下:
注意:NHibernate默认使用代理功能,要求持久化类不是sealed的,而且其公共方法、属性和事件声明为virtual。在这里,类中的字段要设置为virtual,
否则出现“NHibernate.InvalidProxyTypeException”类型的异常在 Shop.Data.dll 中发生,但未在用户代码中进行处理
public  class Customers
{
#region Model private string _customerid;
private string _companyname;
private string _contactname;
private string _contacttitle;
private string _address;
private string _city;
private string _region;
private string _postalcode;
private string _country;
private string _phone;
private string _fax; /// <summary>
/// 客户ID 主键
/// </summary>
public virtual string CustomerID
{
set { _customerid = value; }
get { return _customerid; }
}
/// <summary>
/// 公司
/// </summary>
public virtual string CompanyName
{
set { _companyname = value; }
get { return _companyname; }
}
/// <summary>
/// 客户姓名
/// </summary>
public virtual string ContactName
{
set { _contactname = value; }
get { return _contactname; }
}
/// <summary>
/// 客户头衔
/// </summary>
public virtual string ContactTitle
{
set { _contacttitle = value; }
get { return _contacttitle; }
}
/// <summary>
/// 联系地址
/// </summary>
public virtual string Address
{
set { _address = value; }
get { return _address; }
}
/// <summary>
/// 所在城市
/// </summary>
public virtual string City
{
set { _city = value; }
get { return _city; }
}
/// <summary>
/// 所在地区
/// </summary>
public virtual string Region
{
set { _region = value; }
get { return _region; }
}
/// <summary>
/// 邮编
/// </summary>
public virtual string PostalCode
{
set { _postalcode = value; }
get { return _postalcode; }
}
/// <summary>
/// 国籍
/// </summary>
public virtual string Country
{
set { _country = value; }
get { return _country; }
}
/// <summary>
/// 电话
/// </summary>
public virtual string Phone
{
set { _phone = value; }
get { return _phone; }
}
/// <summary>
/// 传真
/// </summary>
public virtual string Fax
{
set { _fax = value; }
get { return _fax; }
}
#endregion Model
}

7.编写映射文件

编写NHibernate配置文件智能提示的功能。只要在下载的NHibernate里找到configuration.xsd和nhibernate-mapping.xsd两个文件并复制到vs安装目录下,如D:\Program Files (x86)\Microsoft Visual Studio 11.0\Xml\Schemas目录即可。其他的类库类似操作

nhibernate如何知道持久化类和数据库表的对应关系的呢?这就要通过映射文件来完 成这个任务了,映射文件包含了对象/关系映射所需的元数据。元数据包含持久化类的声明和属性到数据库的映射。映射文件告诉nhibernate它应该访问 数据库里面的哪个表及使用表里面的哪些字段。

那么我们编写Customers持久化类的映射文件,注意映射文件以.hbm.xml结尾。如Customers.hbm.xml

我同样使用动软代码生成工具生成映射文件,代码如下:

<?xml version="1.0"  encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Shop.Domain" namespace="Shop.Domain.Entities">
<!--类的全称,程序集,数据库表名称-->
<class name="Shop.Domain.Entities.Customers, Shop.Domain" table="Customers">
<id name="CustomerID" column="CustomerID" type="string" />
<property name="CompanyName" column="CompanyName" type="string" />
<property name="ContactName" column="ContactName" type="string" />
<property name="ContactTitle" column="ContactTitle" type="string" />
<property name="Address" column="Address" type="string" />
<property name="City" column="City" type="string"/>
<property name="Region" column="Region" type="string"/>
<property name="PostalCode" column="PostalCode" type="string"/>
<property name="Country" column="Country" type="string" />
<property name="Phone" column="Phone" type="string" />
<property name="Fax" column="Fax" type="string" />
</class>
</hibernate-mapping>

最后记得给此映射文件设置属性:嵌入的资源

8.添加数据访问层类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Cfg;
using System.Linq.Expressions;
using Shop.Domain.Entities; namespace Shop.Data
{
public class CustomersData
{
/// <summary>
/// 根据条件得到客户信息集合
/// </summary>
/// <param name="where">条件</param>
/// <returns>客户信息集合</returns>
public IList<Customers> GetCustomerList(Expression<Func<Customers, bool>> where)
{
try
{
NHibernateHelper nhibernateHelper = new NHibernateHelper();
ISession session = nhibernateHelper.GetSession();
return session.Query<Customers>().Select(x=>new Customers { ContactName=x.ContactName,City=x.City,Address=x.Address,Phone=x.Phone,CompanyName=x.CompanyName,Country=x.Country}).Where(where).ToList();
}
catch (Exception ex)
{
throw ex;
}
}
}
}

添加业务逻辑层类CustomersBusiness.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Shop.Data;
using Shop.Domain.Entities; namespace Shop.Business
{
public class CustomersBusiness
{
private CustomersData _customersData;
public CustomersBusiness()
{
_customersData = new CustomersData();
}
/// <summary>
/// 根据条件得到客户信息集合
/// </summary>
/// <param name="where">条件</param>
/// <returns>客户信息集合</returns>
public IList<Customers> GetCustomerList(Expression<Func<Customers, bool>> where)
{
return _customersData.GetCustomerList(where);
}
}
}

添加控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shop.Business;
using Shop.Domain.Entities; namespace Shop.Controllers
{
public class CustomersController : Controller
{
CustomersBusiness customersBusiness = new CustomersBusiness();
//
// GET: /Customer/ public ActionResult Index()
{
var result = customersBusiness.GetCustomerList(c => == );
return View(result);
}
}
}

添加视图Index:

@model IEnumerable<Shop.Domain.Entities.Customers>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.CompanyName)
</th>
<th>
@Html.DisplayNameFor(model => model.ContactName)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Country)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th style="width:180px;"></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CompanyName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ContactName)
</td> <td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Country)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>

原文链接:http://www.cnblogs.com/jiekzou/p/4391290.html

在MVC中使用NHibernate学习记录的更多相关文章

  1. ContextLoaderListener和Spring MVC中的DispatcherServlet学习

    DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供Spring Web MVC的集中访问点,并且负责职责的分派,与Spring IoC容器无 ...

  2. ASP.NET MVC中错误日志信息记录

    MVC中有一个处理异常的过滤器 HandleErrorAttribute 1.新建一个类继承自 HandleErrorAttribute,然后重写OnException这个方法 public clas ...

  3. 【转】ASP.NET MVC中错误日志信息记录

    MVC中有一个处理异常的过滤器 HandleErrorAttribute 1.新建一个类继承自 HandleErrorAttribute,然后重写OnException这个方法 public clas ...

  4. python 机器学习中的数据处理学习记录

    在机器学习中,选择合适的算法固然重要,但是数据的处理也同样重要.通过对数据的处理,能提高计算效率,提高预测识别精确度等等 以下记录下一些数据处理的方法 一.处理缺失值 对于数据集中有缺失值的,粗暴的方 ...

  5. mvc中简单的异常记录

    说明:异常处理 1.1 在WebApp的Model中 添加异常处理类 继承于HandleErrorAttribute using System; using System.Collections.Ge ...

  6. ContextLoaderListener和Spring MVC中的DispatcherServlet学习 随手记

    Servlet上下文关系 DispatcherServlet的上下文是通过配置servlet的contextConfigLocation来加载的,默认实现是XmlWebApplicationConte ...

  7. Spring mvc 中 DispatcherServlet 的学习和理解

    上图表示当客户请求来到时,spring架构作出响应的流程,可以从图中看到看到请求分发的中心就是 DispatcherServlet 类,DispatcherServlet的任务是将请求发送给Sprin ...

  8. django中日志使用学习记录

    在setting中加入以下代码 LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'verbose ...

  9. MVC中Filter拦截问题记录之重定向陷阱

    出错环境:被拦截的页面中使用了未实例化的对象,比如只有登录后才有的UserInfor对象. 理想中:浏览器请求页面时,会被Filter拦截,然后重定向到指定页面: 实际现象:将断点打入Filter中, ...

随机推荐

  1. Ubuntu的apt-get本地源搭配——根据需要自己添加软件作源

    试验机器:   Ubuntu 12.04.Lubuntu都可,其他版本应该也没问题,服务器ip:192.168.235.133     主机执行步骤:   1.在/var/cache/apt/arch ...

  2. hdu1084

    #include<iostream> #include<algorithm> using namespace std; #define N 101 struct node { ...

  3. 关于goneaway及499

    关于上面现象的分析如下 问题描述: 接口偶发性出现接口耗时过长的情况 根源: “sockets的快速回收”机制被启动 简单代码+数据分析: 1.      经简单分析,耗时主要出现在连接数据库的方法: ...

  4. React 从入门到进阶之路(三)

    之前的文章我们介绍了 React 创建组件.JSX 语法.绑定数据和绑定对象.接下来我们将介绍 React 绑定属性( 绑定class  绑定style).引入图片  循环数组渲染数据. 上一篇中我们 ...

  5. Ansible Playbooks高级使用

    文件操作 文件创建 file 用于设置文件/链接/目录的属性,或者删除文件/链接/目录 ### state如果是directory当目录不存在时会自动创建:如果是file当文件不存在时不会自动创建 - ...

  6. 洛谷P3533 [POI2012]RAN-Rendezvous

    P3533 [POI2012]RAN-Rendezvous 题目描述 Byteasar is a ranger who works in the Arrow Cave - a famous rende ...

  7. 通过jQuery实现AJAX

    通过jQuery实现AJAX > 使用get和getJSON都会有缓存问题,并且使用get方法不能传送较多的数据. 问题: 在IE浏览器中,get请求使用ajax存在缓存问题,会使用上一次请求的 ...

  8. mariadb yum安装

    安装 yum install mariadb mariadb-server -y 启动 systemctl start mariadb systemctl enable mariadb 安全安装 # ...

  9. 如何顺畅使用sourcetree可视化工具

    http://www.360doc.com/content/17/0711/10/11253639_670493403.shtml sourcetree软件下载 下载地址:https://www.so ...

  10. Java面向对象_常用类库api——日期操作类

    Data类 类Data表示特定的瞬间,精确到毫秒,也就是程序运行时的当前时间 Data data=new Data();//实例化Data对象,表示当前时间 Calendar类 日历类,使用此类可以将 ...