第一个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下载地址:
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!