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

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 { getset; }
4.    public string name { getset; }
5.    public int age { getset; }
6.    public string gender { getset; }
7.    public string email { getset; }
8.    public string phonenum { getset; }
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为嵌入的资源

NHibernate开发入门

增删改查代码

添加命名空间

 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值