欢迎来到【一个大西瓜】的博客

不曾为梦想奋斗,拿什么去燃烧青春。有梦之人亦终将老去,但少年心气如昨。
太阳每一个时刻都同时是夕阳和朝阳,每天她沉入西边,意味着她同时从另一面土地升起。
扩大
缩小

第一个NHibernateDemo

什么是Nhibernate,Nhibernate是一个面向.Net环境的对 象/关系数据库映射工具。(ORM)

1、搭建项目基本框架:

(1)创建MVC项目,命名为NHShop.Web。

(2)依次分别新建3个类库项目:NHShop.Domain,NHShop.Data,NHShop.Business。此项目采用传统三层架构:

  NHShop.Data引用Iesi.Collections.dll、NHibernate.dll和类库NHShop.Domain。

  NHShop.Business引用类库NHShop.Domain和NHShop.Data。

  NHShop.Web需引用Iesi.Collections.dll、NHibernate.dll和类库NHShop.Domain、NHShop.Business。

2、配置数据库连接信息:

  打开packages文件夹,找到NHibernate.4.1.1.4000文件夹下的ConfigurationTemplates文件夹,复制MSSQL.cfg.xml到NHShop.Web跟目录,并重命名为hibernate.xml,注意将部分property改为id

  修改hibernate.xml,修改数据库实例名,并添加mapping节点,修改后如下:

复制代码
<?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 name="NHibernate.Test">
    <!--定制数据库IDriver的类型-->
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <!--连接字符串-->
        <property name="connection.connection_string">
      Server=WYT\SQLEXPRESS;database=Northwind;uid=sa;pwd=123456
    </property>
   <!--NHibernate方言(Dialect)的类名-可以让NHibernate使用某些特定的数据库平台的特性-->
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <!--映射文档中所在的程序集-->
    <mapping assembly="NHShop.Domain" />
    </session-factory>
</hibernate-configuration>
复制代码

 


3、修改hibernate.xml的属性为:如果比较则复制

4、单例模式编写NHibernateHelper辅助类NHibernateHelper:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;

namespace NHShop.Data
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        /// <summary>
        /// 创建ISessionFactory
        /// </summary>
        public static ISessionFactory SessionFactory
        {
            get { 
                //配置ISessionFactory
                return _sessionFactory==null?(new Configuration()).Configure().BuildSessionFactory():_sessionFactory;
            }
        }
    }
}
复制代码

5、NHShop.Domain利用动软代码生成器生成持久化类Customers:

复制代码
using System;

//Nhibernate Code Generation Template 1.0
//author:MythXin
//blog:www.cnblogs.com/MythXin
//Entity Code Generation Template
namespace NHShop.Domain.Entities
{
    //Customers
    public class Customers
    {

        /// <summary>
        /// CustomerID
        /// </summary>
        public virtual string CustomerID
        {
            get;
            set;
        }
        /// <summary>
        /// CompanyName
        /// </summary>
        public virtual string CompanyName
        {
            get;
            set;
        }
        /// <summary>
        /// ContactName
        /// </summary>
        public virtual string ContactName
        {
            get;
            set;
        }
        /// <summary>
        /// ContactTitle
        /// </summary>
        public virtual string ContactTitle
        {
            get;
            set;
        }
        /// <summary>
        /// Address
        /// </summary>
        public virtual string Address
        {
            get;
            set;
        }
        /// <summary>
        /// City
        /// </summary>
        public virtual string City
        {
            get;
            set;
        }
        /// <summary>
        /// Region
        /// </summary>
        public virtual string Region
        {
            get;
            set;
        }
        /// <summary>
        /// PostalCode
        /// </summary>
        public virtual string PostalCode
        {
            get;
            set;
        }
        /// <summary>
        /// Country
        /// </summary>
        public virtual string Country
        {
            get;
            set;
        }
        /// <summary>
        /// Phone
        /// </summary>
        public virtual string Phone
        {
            get;
            set;
        }
        /// <summary>
        /// Fax
        /// </summary>
        public virtual string Fax
        {
            get;
            set;
        }

    }
}
复制代码

6、NHShop.Domain利用动软代码生成器生成映射文件:Customers.hbm.xml,并给文件设置属性:嵌入的资源

复制代码
<?xml version="1.0"  encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHShop.Domain" namespace="NHShop.Domain.Entities">
  <!--类的全称、程序集、数据库表名称-->
  <class name="NHShop.Domain.Entities.Customers, NHShop.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>
复制代码

7、NHShop.Data添加数据访问层类:CustomersData

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHShop.Domain.Entities;
using NHibernate.Linq;
using System.Linq.Expressions;

namespace NHShop.Data
{
    public class CustomersData
    {
        /// <summary>
        /// 根据条件得到客户信息集合
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<Customers> GetCustomerList(Expression<Func<Customers,bool>> where)
        {
            try
            {
                using (ISession session=NHibernateHelper.SessionFactory.OpenSession())
                {
                    return session.Query<Customers>().Select(x => new Customers {
                        CustomerID = x.CustomerID,
                        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;
            }
        }
    }
}
复制代码

8、NHShop.Business添加业务逻辑类:CustomersBusiness

复制代码
using NHShop.Data;
using NHShop.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace NHShop.Business
{
    public class CustomersBusiness
    {
        private CustomersData _customersData;

        public CustomersBusiness()
        {
            _customersData = new CustomersData();
        }

        /// <summary>
        /// 根据条件得到客户信息集合
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<Customers> GetCustomersList(Expression<Func<Customers, bool>> where)
        {
            return _customersData.GetCustomerList(where);
        }
    }
}
复制代码

9、NHShop.Web添加控制器和视图:Customers


最后成功,demo下载地址:

 

点击下载

posted on   一个大西瓜咚咚咚  阅读(309)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

目录导航