NHibernate快速入门

NHibernate使用步骤

1.在web.config的configuration中加入
  <configSections>
    <section
        name="hibernate-configuration"
        type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
        />
  </configSections>
 <!-- hibernate -->
  <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="NHibernate.Test"><!--类似创建EF上下文对象-->
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">
        Server=(local);initial catalog=KSJXC;uid=sa;pwd=123
      </property>
      <property name="adonet.batch_size">10</property>
      <property name="show_sql">false</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="use_outer_join">true</property>
      <property name="command_timeout">60</property>
      <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <!--数据懒加载-->
      <mapping assembly="NHDemo" />  <!--映射程序集名称-->
    </session-factory>
  </hibernate-configuration> 
2.增加一个实体类,注意属性前一定要加virtual ,NH需要生写
public class UserInfo
    {
        public UserInfo()
        {
        }
    
        public virtual string Id { get; set; }
        public virtual string UserName { get; set; }
        public virtual string Password { get; set; }
        public virtual Nullable<bool> IsUse { get; set; }
        public virtual string Tel { get; set; }
        public virtual string Email { get; set; }
        public virtual Nullable<System.DateTime> CreateDate { get; set; }
        public virtual string CreateUser { get; set; }
        public virtual Nullable<System.DateTime> ModifyDate { get; set; }
        public virtual string ModifyUser { get; set; }    
    }


3.增加一个xml文件,一定要以"类.hbm.xml"命名,而且右键属性,生成操作必须选择嵌入的资源
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="NHDemo" assembly="NHDemo">

  <class name="UserInfo" table="UserInfo">
    <id name="Id">  <!--主键-->
      <column name="Id" sql-type="char(10)" not-null="true"/>
    </id>
    <property name="UserName"> <!--属性和字段-->
      <column name="UserName" length="30" not-null="false" />
    </property>
    
    <property name="Password" /> <!--如果属性和字段一样可简写-->
    <property name="IsUse" />    
    <property name="Tel" />
    <property name="Email" />
    <property name="CreateDate" />
    <property name="CreateUser" />
    <property name="ModifyDate" />
    <property name="ModifyUser" />
  </class>
</hibernate-mapping>

4.引入所需要的dll
Antlr3.Runtime.dll
Iesi.Collections.dll
LinFu.DynamicProxy.dll (懒加载,有3种可选Castle,LinFu,Spring)
NHibernate.ByteCode.LinFu.dll (懒加载)
NHibernate.dll
Remotion.Data.Linq.dll
Remotion.Linq.dll
Remotion.Linq.EagerFetching.dll

5.操作数据
A.增加数据
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
            ISession  session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();

            UserInfo ui = new UserInfo();
            ui.Id = "test1";
            ui.Password = King.Comm.WebCommon.GetMd5String("tgj");
            ui.ModifyDate = DateTime.Now;
            ui.CreateDate = DateTime.Now;
            ui.CreateUser = "tgj";
            ui.ModifyUser = "tgd";
            ui.Tel = "137";
            ui.UserName = "kingtang";
            ui.IsUse = true;
            ui.Email = "tgjmail@163.com";
            session.SaveOrUpdate(ui);
            tx.Commit();

            sessionFactory.Close();


B.删除数据
session.Delete(new UserInfo() { Id = "test1" });


C.查询数据
  //查询数据,需要添加命名空间 NHibernate.Linq;
            IQueryable<UserInfo> uis = from u in session.Query<UserInfo>() where u.Id == "tga" select u ;
            foreach (var ui in uis)
            {
                Console.WriteLine(ui.UserName);
            }
            Console.ReadLine();

附:数据库
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[UserInfo](
	[Id] [VARCHAR](10) NOT NULL,
	[UserName] [NVARCHAR](30) NULL,
	[Password] [NCHAR](32) NULL,
	[IsUse] [BIT] NULL,
	[Tel] [VARCHAR](20) NULL,
	[Email] [VARCHAR](50) NULL,
	[CreateDate] [DATETIME] NULL,
	[CreateUser] [NVARCHAR](20) NULL,
	[ModifyDate] [DATETIME] NULL,
	[ModifyUser] [NVARCHAR](20) NULL,
 CONSTRAINT [PK_UserInfo] 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]

GO

SET ANSI_PADDING OFF
GO
说明,在NHibernate-3.0.0通过



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值