Nhibernate学习入门.docx_第1页
Nhibernate学习入门.docx_第2页
Nhibernate学习入门.docx_第3页
Nhibernate学习入门.docx_第4页
Nhibernate学习入门.docx_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Nhibernate学习入门收藏此页 打印作者:jillzhang 2007-03-22 内容导航:环境安装与配置 第1页: 环境安装与配置 第2页: 具体代码 【IT168技术文档】1 学习目的 学习Nhibernate基础知识。掌握Nhibernate的配置方法,实现对单表的简单操作,如:创建表,查询,添加,删除,修改。 2 开发环境+前期准备 开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition 前期准备: Nhibernate框架,我用的目前最新版NHibernate-1.2.0.CR1, 下载地址: /nhibernate/NHibernate-1.2.0.CR1.msi?modtime=1172161735&big_mirror=0 3 开发步骤: 1).双击下载下来的NHibernate-1.2.0.CR1.msi,将其安装到某个目录,我的目录为: E:download projectormnhibernate.,打开该目录,即可以看到bin,doc,src三个子目录,分别为Realse好的dll或者exe目录,文档说明目录,和源程序目录. 2).打开visual studio 2005,创建类库项目NhibernateSample1 3).在解决方案管理其中,右键点击引用-添加引用,在选项卡种选择浏览,设定查找范围为:E:download projectormnhibernatebin,添加对Nhibernate.dll,log4net.dll, Iesi.Collections, HashCodeProvider四个dll的引用. 4).打开SQL Server Management Studio,创建数据库nhibernate。 5).在解决方案管理器中添加hibernate.cfg.xml文件。将下面代码粘贴到此文件: NHibernate.Connection.DriverConnectionProviderNHibernate.Driver.SqlClientDriverServer=;initial catalog=nhibernate;uid=sa;pwd=123;falseNHibernate.Dialect.MsSql2005Dialecttrue 该文件是Nhibernate的配置文件,其中connection.connection_string为数据库连接字符串,Dialect项因为我用的是SQL2005,所以为:MsSql2005Dialect注意:表示映射NhibernateSample1程序集下的所有类,所以以后不要需要Configuration.AddClass(.)了; 6).添加类文件:User.cs,添加代码: using System;using System.Collections.Generic;using System.Text;namespace NhibernateSample1public class Userprivate int _id;private string _name;private string _pwd;/*/ / 编号/ public virtual int Idgetreturn _id;set_id = value;/*/ / 名称/ public virtual string Namegetreturn _name;set_name = value;/*/ / 密码/ public virtual string Pwdgetreturn _pwd;set_pwd = value;6).编写User类的映射配置文件:User.hbm.xml 注意:该映射文件的属性中的生成操作必须为:嵌入的资源. 7).编写管理ISession对象的辅助类: NHibernateHelper.cs,代码为: using System;using System.Web;using NHibernate;using NHibernate.Cfg;namespace NhibernateSample1.public sealed class NHibernateHelper.private static readonly ISessionFactory sessionFactory;static NHibernateHelper().sessionFactory = new Configuration().Configure(E:my projectnhibernate studysimle 1NHibernateStudy1NhibernateSample1hibernate.cfg.xml).BuildSessionFactory();public static ISession GetCurrentSession().ISession currentSession = sessionFactory.OpenSession();return currentSession;public static void CloseSessionFactory().if (sessionFactory != null).sessionFactory.Close();注:因为我用的是单元测试,所以这里的配置文件路径写成固定的了。如果换成windows或者Web程序,可以直接去掉该路径。 8) 编写测试CRUD类:UserFixue using System;using System.Collections.Generic;using System.Text;using NHibernate;using NHibernate.Cfg;using NHibernate.Tool.hbm2ddl;namespace NhibernateSample1public class UserFixureprivate ISession session;public UserFixure()/*/ / 创建表/ public bool ExportTable()tryConfiguration cfg = new Configuration().Configure(E:my projectnhibernate studysimle 1NHibernateStudy1NhibernateSample1hibernate.cfg.xml);session = NHibernateHelper.GetCurrentSession();ITransaction transaction = session.BeginTransaction();new SchemaExport(cfg).Create(true, true);transaction.Commit();return true;catch(Exception ex)throw ex;finallysession.Close();/*/ / 添加/ public int Add()tryUser u = new User();u.Name = Guid.NewGuid().ToString();u.Pwd = 124;session = NHibernateHelper.GetCurrentSession();ITransaction transaction = session.BeginTransaction();session.Save(u);transaction.Commit();return u.Id;catch (Exception ex)throw ex;finallysession.Close();/*/ / 更新/ / public bool Update(int uid)trysession = NHibernateHelper.GetCurrentSession();ITransaction transaction = session.BeginTransaction();User u = session.Load(typeof(User), uid) as User;if (u != null)u.Name = updatedName;session.SaveOrUpdate(u);transaction.Commit();u = session.Load(typeof(User), uid) as User;if (u.Name = updatedName)return true;return false;catch (Exception ex)throw ex;finallysession.Close();/*/ / 删除/ / / public bool Delete(int uid)trysession = NHibernateHelper.GetCurrentSession();ITransaction transaction = session.BeginTransaction();User u = session.Get(typeof(User), uid) as User;if (u != null)session.Delete(u);transaction.Commit();u = session.Get(typeof(User), uid) as User;if (u = null)return true;return false;catch (Exception ex)throw ex;finallysession.Close();public System.Collections.IList Query()trysession = NHibernateHelper.GetCurrentSession();ITransaction transaction = session.BeginTransaction();System.Collections.IList list= session.CreateQuery(select u from User as u).List();transaction.Commit();return list;catch (Exception ex)throw ex;finallysession.Close(); 9)创建新单元测试项目: TestProject1,添加NhibernateSample1的引用 10)创建单元测试类: UnitTest1.cs,并输入如下代码: using System;using System.Text;using System.Collections.Generic;using Microsoft.VisualStudio.TestTools.UnitTesting;using NhibernateSample1;namespace TestProject1/*/ / UnitTest1 的摘要说明/ TestClasspublic class UnitTest1public UnitTest1()/ TODO: 在此处添加构造函数逻辑/ 其他测试属性#region 其他测试属性 / 您可以在编写测试时使用下列其他属性:/ 在运行类中的第一个测试之前使用 ClassInitialize 运行代码/ ClassInitialize()/ public static void MyClassInitialize(TestContext testContext) / 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码/ ClassCleanup()/ public static void MyClassCleanup() / 在运行每个测试之前使用 TestInitialize 运行代码/ TestInitialize()/ public void MyTestInitialize() / 在运行每个测试之后使用 TestCleanup 运行代码/ TestCleanup()/ public void MyTestCleanup() / #endregion int uid;TestMethodpublic void TestMethod1()UserFixure userFixure = new UserFixure();Assert.IsTrue(userFixure.ExportTable();TestMethodpublic void TestMethod2()UserFixure userFixure = new UserFixure();uid = userFixure.Add();Assert.IsTrue(uid0);TestMethodpublic void TestMethod3()UserFixure userFixure = new UserFixure();Assert.IsTrue(userFixure.Update(uid);TestMethodpublic void TestMethod4()UserFixure userFixure = new UserFixure();Assert.IsTrue(userFixure.Delete(uid);TestMethodpublic void TestMethod5()UserFixure

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论