NHibernate入门例子

NHibernate入门例子hello word
首先在数据库建一个users表,代码如下

CREATE TABLE users (
  LogonID nvarchar(
20 ) NOT NULL  default   ' 0 ' ,
  Name nvarchar(
40 default  NULL,
  Password nvarchar(
20 default  NULL,
  EmailAddress nvarchar(
40 default  NULL,
  LastLogon datetime 
default  NULL,
  PRIMARY KEY  (LogonID)
)

然后新建一个我比较擅长的Web应用程序(新建网站),在同一个解决方案中再添加一个C#类库项目,在C#类库项目中创建一个users实体类。
using  System;

namespace  NHibernate.Examples.QuickStart
{
    
public   class  User
    {
        
private   string  id;
        
private   string  userName;
        
private   string  password;
        
private   string  emailAddress;
        
private  DateTime lastLogon;


        
public  User()
        {
        }

        
public   string  Id 
        {
            
get  {  return  id; }
            
set  { id  =  value; }
        }

        
public   string  UserName 
        {
            
get  {  return  userName; }
            
set  { userName  =  value; }
        }

        
public   string  Password 
        {
            
get  {  return  password; }
            
set  { password  =  value; }
        }

        
public   string  EmailAddress 
        {
            
get  {  return  emailAddress; }
            
set  { emailAddress  =  value; }
        }

        
public  DateTime LastLogon 
        {
            
get  {  return  lastLogon; }
            
set  { lastLogon  =  value; }
        }
        
    }
}

接着就是 NHibernate的影射文件(Mapping file),只需新建一个xml文档,改名为 User.hbm.xml(与User类对应),然后把他的生成操作设为嵌入资源。代码如下
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
< hibernate - mapping xmlns = " urn:nhibernate-mapping-2.2 " >
    
< class  name = " NHibernate.Examples.QuickStart.User, NHibernate.Examples "  table = " users " >
        
< id name = " Id "  column = " LogonId "  type = " String "  length = " 20 " >  
            
< generator  class = " assigned "   />  
        
</ id >  
        
< property name = " UserName "  column = " Name "  type = " String "  length = " 40 " />  
        
< property name = " Password "  type = " String "  length = " 20 " />  
        
< property name = " EmailAddress "  type = " String "  length = " 40 " />
        
< property name = " LastLogon "  type = " DateTime " />
    
</ class >
</ hibernate - mapping >

接着就是编辑配置文件,因为是WebApplication,所以在Web.config的<configuration> 配置节中加入以下内容,注意<configSections>要作为<configuration> 的第一个元素。
  < configSections >
    
< section
      name
= " nhibernate "  
      type
= " System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "  
    
/>
  
</ configSections >

  
< nhibernate >
    
< add 
      key
= " hibernate.connection.provider "
      value
= " NHibernate.Connection.DriverConnectionProvider "
    
/>
    
< add
      key
= " hibernate.dialect "
      value
= " NHibernate.Dialect.MsSql2000Dialect "
    
/>
    
< add
      key
= " hibernate.connection.driver_class "
      value
= " NHibernate.Driver.SqlClientDriver "
    
/>
    
< add
      key
= " hibernate.connection.connection_string "
      value
= " Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI "
    
/>
  
</ nhibernate >

就只剩下最后一步了,在web项目里添加一个test1.aspx的页面,添上以下代码,编译,通过.....
using  System;
using  System.Data;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  NHibernate;
using  NHibernate.Cfg;
public  partial  class  test1 : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        Configuration cfg 
=   new  Configuration();
        cfg.AddAssembly(
" NHibernate.Examples " );
        ISessionFactory factory 
=  cfg.BuildSessionFactory();
        ISession session 
=  factory.OpenSession();
        ITransaction transaction 
=  session.BeginTransaction();
        NHibernate.Examples.QuickStart.User newUser 
=   new  NHibernate.Examples.QuickStart.User();
        newUser.Id 
=   " joe_cool1 " ;
        newUser.UserName 
=   " Joseph Cool1 " ;
        newUser.Password 
=   " abc1231 " ;
        newUser.EmailAddress 
=   " joe@cool.com1 " ;
        newUser.LastLogon 
=  DateTime.Now;
        session.Save(newUser);
        transaction.Commit();
        session.Close();
    }
}

运行web,看一下数据库,呵呵,数据被插入了

其中有几点需要注意:

1、User.hbm.xml这个文件要作为镶入到User的项目中,只需在User.hbm.xml文件上按右

转载于:https://www.cnblogs.com/huangshuai/archive/2008/06/18/1224859.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值