首先,咱們瞭解一下ORM是什麼?
ORM指對象關係映射(英語:Object Relational Mapping,簡稱ORM,或O/RM,或O/R mapping),是一種程序技術,用於實現面向對象編程語言裏不一樣類型系統的數據之間的轉換。從效果上說,它實際上是建立了一個可在編程語言裏使用的「虛擬對象數據庫」。html
其次,咱們須要瞭解.NET領域ORM框架有哪些?
在.NET平臺下,關於數據持久層框架很是多,如下是主要的5種:
1.NHibernate
2.NBear
3.Castle ActiveRecord
4.iBATIS.NET
5.DAAB 微軟sql
使用過Java的朋友都不會對Hibernate陌生。因爲以前的Java經歷,因此我直接選擇NHibernate做爲ORM框架首選。那麼NHibernate是什麼?數據庫
NHibernate是一個面向.NET環境的對象/關係數據庫映射工具。對象/關係數據庫映射(object/relational mapping,ORM)這個術語表示一種技術,用來把對象模型表示的對象映射到基於SQL的關係模型數據結構中去。編程
下面,咱們作一個簡單的NHibernate Demo。建立項目WinForm窗體項目,命名爲CRMdemo,如下是解決方案截圖:session
解決方案項目截圖數據結構
NHibernate下載地址app
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/ide
爲了方便,下載後將NHibernate-4.0.3.GA-bin.zip解壓縮至解決方案根目錄下
添加引用
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
複製NHibernate-4.0.3.GA-bin\Configuration_Templates\MSSQL.cfg.xml至bin\Debug下,重命名爲hibernate.cfg.xml,並作以下配置
<!-- This is the System.Data.dll provider for SQL Server --> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="CRMDemo.Hibernate"> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Server=Your-PC\SQLEXPRESS;initial catalog=crm;Integrated Security=SSPI </property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <mapping assembly="CRMDemo"/> </session-factory> </hibernate-configuration>
建立員工表employee
CREATE TABLE [dbo].[employee]( [id] [int] IDENTITY(1,1) NOT NULL, [name] [nvarchar](50) NULL, [age] [nchar](10) NULL, [gender] [nchar](10) NULL, [email] [nvarchar](100) NULL, [phonenum] [nvarchar](50) NULL, CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
public class Employee { public virtual int id { get; set; } public virtual string name { get; set; } public virtual int age { get; set; } public virtual string gender { get; set; } public virtual string email { get; set; } public virtual string phonenum { get; set; } }
<?xml version="1.0" ?> <!-- * By huiyaosoft.com * build date 2015-4-12 10:10 --> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> <class name="CRMDemo.Employee, CRMDemo" discriminator-value="0" table="employee" > <id name="id" type="int" unsaved-value="null"> <column name="id" length="4" sql-type="4" not-null="true" unique="true" index="PK_employee"/> <generator class="native" /> <!-- unsaved-value used to be null and generator was increment in h2.0.3 --> </id> <property name="name" type="String"> <column name="name" length="50" sql-type="nvarchar" not-null="false"/> </property> <property name="email" type="String"> <column name="email" length="50" sql-type="nvarchar" not-null="false"/> </property> <property name="gender" type="String"> <column name="gender" length="10" sql-type="nchar" not-null="false"/> </property> <property name="phonenum" type="String"> <column name="phonenum" length="50" sql-type="nvarchar" not-null="false"/> </property> <property name="age" type="int"> <column name="age" length="4" sql-type="int" not-null="false"/> </property> </class> </hibernate-mapping>
設置Employee.hbm.xml爲嵌入的資源
添加命名空間
using NHibernate; using NHibernate.Cfg;
查詢操做,CreateQuery或CreateSqlQuery
Configuration cfg = new Configuration(); cfg.Configure(); ISessionFactory factory = cfg.BuildSessionFactory(); ISession session = factory.OpenSession(); IQuery query = session.CreateQuery("from Employee"); IList<Employee> eList = query.List<Employee>(); foreach (Employee item in eList) { this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); } //ISQLQuery query = session.CreateSQLQuery("select * from employee").AddEntity(typeof(Employee)); //IList<Employee> eList = query.List<Employee>(); //foreach (Employee item in eList) //{ // Console.WriteLine("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email); //} session.Close(); factory.Close();
增長操做session.Save(Entity)
Configuration cfg = new Configuration(); cfg.Configure(); ISessionFactory factory = cfg.BuildSessionFactory(); ISession session = factory.OpenSession(); ITransaction trans = session.BeginTransaction(); Employee ee = new Employee(); ee.age = 31; ee.name = "李四"; ee.email = "zhang@163.com"; ee.phonenum = "1358111111"; ee.gender = "男"; session.Save(ee); trans.Commit(); session.Close(); factory.Close(); this.textBox1.AppendText("已增長一條記錄\r\n");
修改操做session.Save(Entity)
Configuration cfg = new Configuration(); cfg.Configure(); ISessionFactory factory = cfg.BuildSessionFactory(); ISession session = factory.OpenSession(); ITransaction trans = session.BeginTransaction(); IQuery query = session.CreateQuery("from Employee where id = :id").SetString("id","1"); IList<Employee> eList = query.List<Employee>(); foreach (Employee item in eList) { this.textBox1.AppendText("查詢到一條記錄\r\n"); this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); item.email = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); session.Save(item); } trans.Commit(); query = session.CreateQuery("from Employee where id = :id").SetString("id", "1"); eList = query.List<Employee>(); foreach (Employee item in eList) { this.textBox1.AppendText("修改後記錄\r\n"); this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); } session.Close(); factory.Close();
刪除操做session.Delete(Entity)
Configuration cfg = new Configuration(); cfg.Configure(); ISessionFactory factory = cfg.BuildSessionFactory(); ISession session = factory.OpenSession(); ITransaction trans = session.BeginTransaction(); IQuery query = session.CreateQuery("from Employee where name = :name").SetString("name", "李四"); IList<Employee> eList = query.List<Employee>(); foreach (Employee item in eList) { this.textBox1.AppendText("查詢記錄\r\n"); this.textBox1.AppendText(string.Format("id:{0},name:{1},email:{2}\r\n", item.id, item.name, item.email)); session.Delete(item); } trans.Commit(); query = session.CreateQuery("from Employee where name = :name").SetString("name", "李四"); eList = query.List<Employee>(); foreach (Employee item in eList) { this.textBox1.AppendText("刪除後記錄\r\n"); } session.Close(); factory.Close();
來源:http://www.huiyaosoft.com/html/nhibernate.htm
本項目演示例子點擊下載