[Solution] ORM NHibernate系列(1) 快速入门

  NHibernate是一个面向.NET环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。

  官网:http://www.nhforge.org/

  下载:Install-Package NHibernate

 

下载并引用程序集

 

 

 

http://nhibernate.info/doc/nh/en/index.html#quickstart-intro

 

推荐可以看看:

NHibernate从入门到精通系列(刘冬)

 

NHibernate之旅

 

在NHibernate中使用memcache二级缓存

 

如果手写配置文件,拷贝2个xsd文件可自动提示:

nhibernate-mapping.xsd

nhibernate-configuration.xsd

 

NHibernate步骤:

1.poco

 1 using System;

 2 

 3 namespace NHCRUD.Model

 4 {

 5     public class Product

 6     {

 7         public virtual int Id { get; set; }

 8         public virtual string PName { get; set; }

 9         public virtual decimal Price { get; set; }

10     }

11 }
POCO

 

2.poco.hbm.xml(嵌入的资源)

 1 <?xml version="1.0" encoding="utf-8" ?>

 2 

 3 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHCRUD" namespace="NHCRUD.Model">

 4   <class name="Product" table="T_Product" lazy="true" >

 5     <id name="Id" column="ID" type="int" >

 6       <generator class="assigned" />

 7     </id>

 8 

 9     <property name="PName" type="string">

10       <column name="Name" length="50"/>

11     </property>

12 

13     <property name="Price" type="decimal">

14       <column name="BuyPrice" precision="14" scale="2"/>

15     </property>

16   </class>

17 </hibernate-mapping>
poco.hbm.xml

 

3.引入NHibernate程序集

 

4.cfg.xml(始终复制)

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >

 3   <session-factory name="NHCRUD">

 4     <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>

 5     <property name="connection.connection_string">{连接字符串}</property>

 6     <property name="show_sql">false</property>

 7     <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>

 8     <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>

 9     <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate</property>

10     <property name="hbm2ddl.auto">update</property>

11     <mapping assembly="{POCO的程序集}"/>

12   </session-factory>

13 </hibernate-configuration>
Cfg.xml

 

5.ISessionFactory:

var cfg = new NHibernate.Cfg.Configuration().Configure("hb.xml");
var sessionFactory = cfg.BuildSessionFactory();

 

6.CRUD

 1 using (var session = sessionFactory.OpenSession())

 2 {

 3 

 4   //add

 5 

 6   session.Save(new Model.Product { Id = 1, PName = "手机", Price = 19M });

 7 

 8 

 9   //del

10 

11   session.Save(new Model.Product { Id = 1, PName = "手机", Price = 19M });

12 

13   //update

14 

15   session.Update(new Model.Product { Id = 1, PName = "电脑", Price = 18M });

16 

17   //Get

18 

19   session.Get<Model.Product>(1);

20 

21   //Get

22 

23   session.Get(typeof(Model.Product),1);

24 

25 

26   //List

27 

28   session.CreateQuery("from Product").List<Model.Product>();

29 

30     

31   //PageList

32 

33   session.CreateQuery("from Product").List<Model.Product>().Skip(10).Take(10);

34 

35 

36 

37   session.Flush();

38 

39 

40 }
CRUD

 

你可能感兴趣的:(Hibernate)