NHibernate 做个小项目来试一下吧 一

Posted on 2004-10-14 16:49 无心之柳.NET 阅读(412) 评论(3)   编辑  收藏

只有在实际运用中才能真正的遇到问题 ,并在解决问题的过程中不断提高
在网上参考了 http://blog.aspcool.com/tim/posts/1133.aspx
                          http://nhibernate.3yee.com/archive/2004/04/26/439.aspx
等多篇文章后
所以 打算写基于NHibernate 的一个小小的留言本,
首先 建数据库

None.gif
None.gif
CREATE   TABLE   [ dbo ] . [ G_guestbook ]  (
None.gif    
[ id ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
None.gif    
[ userid ]   [ int ]   NOT   NULL  ,
None.gif    
[ username ]   [ nvarchar ]  ( 20 ) COLLATE Chinese_PRC_CI_AS  NOT   NULL  ,
None.gif    
[ title ]   [ nvarchar ]  ( 100 ) COLLATE Chinese_PRC_CI_AS  NOT   NULL  ,
None.gif    
[ article ]   [ ntext ]  COLLATE Chinese_PRC_CI_AS  NULL  ,
None.gif    
[ pubtime ]   [ datetime ]   NOT   NULL  
None.gif
ON   [ PRIMARY ]  TEXTIMAGE_ON  [ PRIMARY ]
None.gif
GO
None.gif
None.gif
CREATE   TABLE   [ dbo ] . [ G_users ]  (
None.gif    
[ id ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
None.gif    
[ Name ]   [ nvarchar ]  ( 20 ) COLLATE Chinese_PRC_CI_AS  NOT   NULL  ,
None.gif    
[ email ]   [ nvarchar ]  ( 40 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
None.gif    
[ password ]   [ nvarchar ]  ( 20 ) COLLATE Chinese_PRC_CI_AS  NOT   NULL  ,
None.gif    
[ flag ]   [ int ]   NOT   NULL  ,
None.gif    
[ regtime ]   [ datetime ]   NULL  
None.gif)

很简单的结构,可以用就行了, teeth_smile.gif
第二步是打开 VS.net 新建一个
guestbook.data的工程 ,在这里不得不感谢飞鹰,提供了cool coder这个好东东啊, 虽然还不是哪么的完美,但是生成的实体类,各 映射文件只要稍加改动就可以加到 我的工程里了,节省了大量的时间啊,
我的两个实体类以及 映射文件
None.gif using  System;
None.gif
None.gif
namespace  guestbook.data
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class  guestbooks
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public guestbooks()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private System.String _article;
InBlock.gif        
public System.String article
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _article; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _article = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Int32 _id;
InBlock.gif        
public System.Int32 id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _id = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.DateTime _pubtime;
InBlock.gif        
public System.DateTime pubtime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _pubtime; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _pubtime = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.String _title;
InBlock.gif        
public System.String title
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _title; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _title = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Int32 _userid;
InBlock.gif        
public System.Int32 userid
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _userid; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _userid = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.String _username;
InBlock.gif        
public System.String username
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _username; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _username = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

None.gif using  System;
None.gif
None.gif
namespace  guestbook.data
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class  users
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public users()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private System.Int32 _flag;
InBlock.gif        
public System.Int32 flag
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _flag; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _flag = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.String _email;
InBlock.gif        
public System.String email
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _email; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _email = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Int32 _id;
InBlock.gif        
public System.Int32 id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _id = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.DateTime _regtime;
InBlock.gif        
public System.DateTime regtime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _regtime; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _regtime = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.String _Name;
InBlock.gif        
public System.String Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _Name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.String _password;
InBlock.gif        
public System.String password
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
get dot.gifreturn _password; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _password = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
还有两个映射文件
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif    
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.0" >
None.gif        
< class  name ="guestbook.data.users, guestbook.data"  table ="G_users" >
None.gif                
< id  name ="id"  column ="id"  type ="Int32" >
None.gif                        
< generator  class ="identity"   />
None.gif                
</ id >
None.gif            
< property  name ="Name"  type ="String(20)"  column ="Name"   />
None.gif            
< property  name ="email"  type ="String(40)"  column ="email"   />
None.gif            
< property  name ="password"  type ="String(20)"  column ="password"   />
None.gif            
< property  name ="flag"  type ="Int32"  column ="flag"   />
None.gif            
< property  name ="regtime"  type ="DateTime"  column ="regtime"   />
None.gif            
</ class >
None.gif
</ hibernate-mapping >
None.gif
None.gif
<? xml version="1.0" encoding="utf-8"  ?>
None.gif    
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.0" >
None.gif        
< class  name ="guestbook.data.guestbooks, guestbook.data"  table ="G_guestbook" >
None.gif                
< id  name ="id"  column ="id"  type ="Int32" >
None.gif                        
< generator  class ="identity"   />
None.gif                
</ id >
None.gif            
< property  name ="userid"  type ="Int32"  column ="userid"   />
None.gif            
< property  name ="username"  type ="String(20)"  column ="username"   />
None.gif            
< property  name ="title"  type ="String(100)"  column ="title"   />
None.gif            
< property  name ="article"  type ="String"  column ="article"   />
None.gif            
< property  name ="pubtime"  type ="DateTime"  column ="pubtime"   />
None.gif            
</ class >
None.gif
</ hibernate-mapping >
由于我用的是自动编号,所以,要将 <generator class="assigned" />改为  <generator class="identity"" />
为了能更早的了解代码是否正确,免除只能把界面搭建出来以后才能测试一个小模块的代码的尴尬,所以 请出了,NUnit 虽然我还不是怎么会用,但是一个能你让还不怎么对它怎么熟悉,但就觉得非常好用的软件,实在是太强了 thumbs_up.gif
添一个新建项目 命名为 guestbook.test
写一个测试文件
这里的主要目地是为了验证 配置的 正确与否,所以测试文件也写的简单一些了,
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  NHibernate;
None.gif
using  NHibernate.Cfg;
None.gif
None.gif
using  NUnit.Framework;
None.gif
None.gif
using  guestbook.data;
None.gif
None.gif
namespace  guestbook.test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DataLayerUsersTest 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class DataLayerUsersTest
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif        
public DataLayerUsersTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Test]
public void testAdd()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Configuration cfg
=new Configuration();
InBlock.gif            cfg.AddXmlFile(
"users.hbm.xml");
InBlock.gif
InBlock.gif            ISessionFactory f
=cfg.BuildSessionFactory();
InBlock.gif            ISession s
=f.OpenSession();
InBlock.gif            ITransaction t
=s.BeginTransaction();
InBlock.gif
InBlock.gif            users newUser
=new users();
InBlock.gif            newUser.Name
="papersnake";
InBlock.gif            newUser.password
="24976904";
InBlock.gif            newUser.email
="papersnakes@gmail.com";
InBlock.gif            newUser.regtime
=DateTime.Now;
InBlock.gif
InBlock.gif            s.Save(newUser);
InBlock.gif
InBlock.gif            t.Commit();
InBlock.gif            s.Close();
InBlock.gif
InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
另一个文件也大同小意不放出来了,编译
打开Nunit ,run 一下,出现一道美丽的绿色。
心情不是一班的好啊,哇卡卡

摘于 http://www.cnblogs.com/9527/archive/2004/10/14/52326.html

转载于:https://www.cnblogs.com/ts_xmx/archive/2004/11/08/61516.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值