NHibernate开发入门
首先,我们了解一下ORM是什么?
ORM指对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。从效果上说,它其实是创建了一个可在编程语言里使用的“虚拟对象数据库”。
其次,我们需要了解.NET领域ORM框架有哪些?
在.NET平台下,关于数据持久层框架非常多,以下是主要的5种:
1.NHibernate
2.NBear
3.Castle ActiveRecord
4.iBATIS.NET
5.DAAB 微软
使用过Java的朋友都不会对Hibernate陌生。由于之前的Java经历,所以我直接选择NHibernate作为ORM框架首选。那么NHibernate是什么?
NHibernate是一个面向.NET环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。
下面,我们做一个简单的NHibernate Demo。创建项目WinForm窗体项目,命名为CRMdemo,以下是解决方案截图:
解决方案项目截图

获取NHibernate
NHibernate下载地址
https://sourceforge.net/projects/nhibernate/files/NHibernate/4.0.3.GA/NHibernate-4.0.3.GA-bin.zip/download
NHibernate项目地址
https://sourceforge.net/projects/nhibernate/
为了方便,下载后将NHibernate-4.0.3.GA-bin.zip解压缩至解决方案根目录下
添加NHibernate Dll引用
添加引用
NHibernate-4.0.3.GA-bin\Required_Bins\NHibernate.dll
NHibernate-4.0.3.GA-bin\Required_Bins\Iesi.Collections.dll
NHibernate-4.0.3.GA-bin\Tests\log4net.dll
NHibernate-4.0.3.GA-bin\Tests\NHibernate.DomainModel.dll
NHibernate-4.0.3.GA-bin\Tests\nunit.framework.dll
配置hibernate.cfg.xml
复制NHibernate-4.0.3.GA-bin\Configuration_Templates\MSSQL.cfg.xml至bin\Debug下,重命名为hibernate.cfg.xml,并做如下配置
| C# Code | |
| 1. | <!-- This is the System.Data.dll provider for SQL Server --> |
| 2. | <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > |
| 3. | <session-factory name="CRMDemo.Hibernate"> |
| 4. | <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> |
| 5. | <property name="connection.connection_string"> |
| 6. | Server=Your-PC\SQLEXPRESS;initial catalog=crm;Integrated Security=SSPI |
| 7. | </property> |
| 8. | <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> |
| 9. | <mapping assembly="CRMDemo"/> |
| 10. | </session-factory> |
| 11. | </hibernate-configuration> |
在MSSQL中创建一张演示表employee
创建员工表employee
| C# Code | |
| 1. | CREATE TABLE [dbo].[employee]( |
| 2. | [id] [int] IDENTITY(1,1) NOT NULL, |
| 3. | [name] [nvarchar](50) NULL, |
| 4. | [age] [nchar](10) NULL, |
| 5. | [gender] [nchar](10) NULL, |
| 6. | [email] [nvarchar](100) NULL, |
| 7. | [phonenum] [nvarchar](50) NULL, |
| 8. | CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED |
| 9. | ( |
| 10. | [id] ASC |
| 11. | )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] |
| 12. | ) ON [PRIMARY] |
创建Employee.cs类
| C# Code | |
| 1. | public class Employee |
| 2. | { |
| 3. | public int id { get; set; } |
| 4. | public string name { get; set; } |
| 5. | public int age { get; set; } |
| 6. | public string gender { get; set; } |
| 7. | public string email { get; set; } |
| 8. | public string phonenum { get; set; } |
| 9. | } |
创建映射文件Employee.hbm.xml,其中字段名称必须和类属性名称一致,区分大小写。
| C# Code | |
| 1. | <?xml version="1.0" ?> |
| 2. | <!-- |
| 3. | * By huiyaosoft.com |
| 4. | * build date 2015-4-12 10:10 |
| 5. | --> |
| 6. | <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> |
| 7. | <class |
| 8. | name="CRMDemo.Employee, CRMDemo" |
| 9. | discriminator-value="0" table="employee" |
| 10. | > |
| 11. | <id |
| 12. | name="id" type="int" |
| 13. | unsaved-value="null" |
| 14. | > |
| 15. | <column name="id" length="4" sql-type="4" not-null="true" unique="true" |
| 16. | index="PK_employee"/> |
| 17. | <generator class="native" /> |
| 18. | <!-- unsaved-value used to be null and generator was increment in h2.0.3 --> |
| 19. | </id> |
| 20. | |
| 21. | <property name="name" type="String"> |
| 22. | <column name="name" length="50" sql-type="nvarchar" not-null="false"/> |
| 23. | </property> |
| 24. | <property name="email" type="String"> |
| 25. | <column name="email" length="50" sql-type="nvarchar" not-null="false"/> |
| 26. | </property> |
| 27. | <property name="gender" type="String"> |
| 28. | <column name="gender" length="10" sql-type="nchar" not-null="false"/> |
| 29. | </property> |
| 30. | <property name="phonenum" type="String"> |
| 31. | <column name="phonenum" length="50" sql-type="nvarchar" not-null="false"/> |
| 32. | </property> |
| 33. | <property name="age" type="int"> |
| 34. | <column name="age" length="4" sql-type="int" not-null="false"/> |
| 35. | </property> |
| 36. | </class> |
| 37. | </hibernate-mapping> |
设置Employee.hbm.xml为嵌入的资源

增删改查代码
添加命名空间
| C# Code | |
| 1. | using NHibernate; |
| 2. | using NHibernate.Cfg; |
查询操作,CreateQuery或CreateSqlQuery
| C# Code | |
| 1. | Configuration cfg = new Configuration(); |
| 2. | cfg.Configure(); |
| 3. | ISessionFactory factory = cfg.BuildSessionFactory(); |
| 4. | ISession session = factory.OpenSession(); |
| 5. | IQuery query = session.CreateQuery("from Employee"); |
| 6. | IList<Employee> eList = query.List<Employee>(); |
| 7. | foreach (Employee item in eList) |
| 8. | { |
| 9. | this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); |
| 10. | } |
| 11. | //ISQLQuery query = session.CreateSQLQuery("select * from employee").AddEntity(typeof(Employee)); |
| 12. | //IList<Employee> eList = query.List<Employee>(); |
| 13. | //foreach (Employee item in eList) |
| 14. | //{ |
| 15. | // Console.WriteLine("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email); |
| 16. | //} |
| 17. | session.Close(); |
| 18. | factory.Close(); |
增加操作session.Save(Entity)
| C# Code | |
| 1. | Configuration cfg = new Configuration(); |
| 2. | cfg.Configure(); |
| 3. | ISessionFactory factory = cfg.BuildSessionFactory(); |
| 4. | ISession session = factory.OpenSession(); |
| 5. | ITransaction trans = session.BeginTransaction(); |
| 6. | Employee ee = new Employee(); |
| 7. | ee.age = 31; |
| 8. | ee.name = "李四"; |
| 9. | ee.email = "zhang@163.com"; |
| 10. | ee.phonenum = "1358111111"; |
| 11. | ee.gender = "男"; |
| 12. | session.Save(ee); |
| 13. | trans.Commit(); |
| 14. | session.Close(); |
| 15. | factory.Close(); |
| 16. | this.textBox1.AppendText("已增加一条记录\r\n"); |
修改操作session.Save(Entity)
| C# Code | |
| 1. | Configuration cfg = new Configuration(); |
| 2. | cfg.Configure(); |
| 3. | ISessionFactory factory = cfg.BuildSessionFactory(); |
| 4. | ISession session = factory.OpenSession(); |
| 5. | ITransaction trans = session.BeginTransaction(); |
| 6. | IQuery query = session.CreateQuery("from Employee where id = :id").SetString("id","1"); |
| 7. | IList<Employee> eList = query.List<Employee>(); |
| 8. | foreach (Employee item in eList) |
| 9. | { |
| 10. | this.textBox1.AppendText("查询到一条记录\r\n"); |
| 11. | this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); |
| 12. | item.email = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| 13. | session.Save(item); |
| 14. | } |
| 15. | trans.Commit(); |
| 16. | query = session.CreateQuery("from Employee where id = :id").SetString("id", "1"); |
| 17. | eList = query.List<Employee>(); |
| 18. | foreach (Employee item in eList) |
| 19. | { |
| 20. | this.textBox1.AppendText("修改后记录\r\n"); |
| 21. | this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); |
| 22. | } |
| 23. | session.Close(); |
| 24. | factory.Close(); |
删除操作session.Delete(Entity)
| C# Code | |
| 1. | Configuration cfg = new Configuration(); |
| 2. | cfg.Configure(); |
| 3. | ISessionFactory factory = cfg.BuildSessionFactory(); |
| 4. | ISession session = factory.OpenSession(); |
| 5. | ITransaction trans = session.BeginTransaction(); |
| 6. | IQuery query = session.CreateQuery("from Employee where name = :name").SetString("name", "李四"); |
| 7. | IList<Employee> eList = query.List<Employee>(); |
| 8. | foreach (Employee item in eList) |
| 9. | { |
| 10. | this.textBox1.AppendText("查询记录\r\n"); |
| 11. | this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); |
| 12. | session.Delete(item); |
| 13. | } |
| 14. | trans.Commit(); |
| 15. | query = session.CreateQuery("from Employee where name = :name").SetString("name", "李四"); |
| 16. | eList = query.List<Employee>(); |
| 17. | foreach (Employee item in eList) |
| 18. | { |
| 19. | this.textBox1.AppendText("删除后记录\r\n"); |
| 20. | } |
| 21. | session.Close(); |
| 22. | factory.Close(); |
参考文献
http://www.cnblogs.com/stone_w/archive/2011/09/15/2177830.html
本项目演示例子点击下载
NHibernate开发入门的更多相关文章
- .Net Core ORM选择之路,哪个才适合你 通用查询类封装之Mongodb篇 Snowflake(雪花算法)的JavaScript实现 【开发记录】如何在B/S项目中使用中国天气的实时天气功能 【开发记录】微信小游戏开发入门——俄罗斯方块
.Net Core ORM选择之路,哪个才适合你 因为老板的一句话公司项目需要迁移到.Net Core ,但是以前同事用的ORM不支持.Net Core 开发过程也遇到了各种坑,插入条数多了也特别 ...
- openresty 前端开发入门五之Mysql篇
openresty 前端开发入门五之Mysql篇 这章主要演示怎么通过lua连接mysql,并根据用户输入的name从mysql获取数据,并返回给用户 操作mysql主要用到了lua-resty-my ...
- java WEB开发入门
WEB开发入门 1 进入web JAVASE:标准- standard JAVA桌面程序 GUI SOCKET JAVAEE:企业-浏览器控制 web 2 软件结构 C/S :client ...
- [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解
原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...
- [译]:Xamarin.Android开发入门——Hello,Android深入理解
返回索引目录 原文链接:Hello, Android_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android深入理解 本部分介绍利用Xamarin开发And ...
- [译]:Xamarin.Android开发入门——Hello,Android快速上手
返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...
- VR原理讲解及开发入门
本文是作者obuil根据多年心得专门为想要入门的VR开发者所写,由52VR网站提供支持. 1. VR沉浸感和交互作用产生的原理: 在之前,我们观看一个虚拟的创造内容是通过平面显示器的,52VR ...
- Eclipse_luna_J2EE_For_JS+tomcat8.0环境搭建、配置、开发入门
一.所有需要的软件.插件等下载地址 J2SE的官方下载路径:http://www.oracle.com/technetwork/java/javase/downloads/index.html Ecl ...
- OWIN的理解和实践(三) –Middleware开发入门
上篇我们谈了Host和Server的建立,但Host和Server无法产出任何有实际意义的内容,真正的内容来自于加载于Server的Middleware,本篇我们就着重介绍下Middleware的开发 ...
随机推荐
- COM 组件基础——GUID 和 接口
一.前言 书接上回,话说在 doc(Word) 复合文件中,已经解决了保存 xls(Excel) 数据的问题了.那么,接下来又要解决另一个问题:当 WORD 程序读取复合文件,遇到了 xls 数据的时 ...
- eafier 簡單易用 HTML、CSS 網頁編輯器(可自動插入 Tag 標籤)
很久很久以前,一般人要做網頁的話大概都會用 FrontPage 之類的工具,更進階一點的會用 Dreamweaver 等軟體.雖然上述軟體提供很方便的「所見即所得」的編輯預覽模式,但寫出來的網頁程式碼 ...
- 搭建Apache Web服务器
1.下载Apache服务器的安装包 地址:http://httpd.apache.org/download.cgi 从http://archive.apache.org/dist/httpd/bina ...
- YII 错误 SQLSTATE[HY000] [2002] No such file or directory
在使用yii的yii\db\Connnection时发生错误 <?php namespace app\controllers; use yii\web\Controller; use yii\d ...
- break与continue的区别
break 在while.for.do...while.while循环中使用break语句退出当前循环,直接执行后面的代码. continue 的作用是仅仅跳过本次循环,而整个循环体继 ...
- 常用vim插件的安装、使用和管理
1.Ctags Ctags工具是用来遍历源代码文件生成tags文件,这些tags文件能被编辑器或者其他工具用来快速查找定位源代码中的符号,入变量名,函数名等.比如,tags文件就是Taglist和 ...
- filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )
很多时候需要将图片显示在网页上,一般都会这样做,如下: <img src="xxx.jpg"/> 是的,这样是可以做到,但是如果我要将本地的图片显示到页面上呢?你可能会 ...
- Spark基础知识汇总
2,wordcount: val wordcount = sc.textFile()).reduceByKey(_ + _).sortByKey().collect val wordcount = s ...
- Python flask @app.route
转载自 http://python.jobbole.com/80956/ 下面是Flask主页给我们的第一个例子,我们现在就由它入手,深入理解“@app.route()”是如何工作的. ...
- 转:IOC框架
CSND上看了王泽滨的博客关于IOC的,觉得说的很透彻,地址为:http://blog.csdn.net/wanghao72214/article/details/3969594 1 IoC理论的背景 ...