博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的第一个NHibernate示例
阅读量:6991 次
发布时间:2019-06-27

本文共 6853 字,大约阅读时间需要 22 分钟。

整个项目的结构图如下:

1、开发工具Microsoft Visual Studio2010、SQL Server2012

2、NHibernate版本为3.3.1

3、新建类库GaoJie.Domain,在该类库项目下新建两个文件夹Domain和Mapping。

编写Student类,该类在Domain文件夹下。代码如下:

View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace GaoJie.Domain.Domain 7 { 8     ///  9     /// Student10     /// 11     public class Student12     {13         /// 14         /// Id15         /// 16         public virtual Guid Id { set; get; }17 18         /// 19         /// Name20         /// 21         public virtual string Name { set; get; }22 23         /// 24         /// Age25         /// 26         public virtual int Age { set; get; }27     }28 }

 

编写映射文件Student.hbm.xml,将该文件的属性设置为,如图所示:

代码如下:

View Code
1 
2 3
4 5
6 7
8
9
10 11
12
13
14 15
16
17
18 19
20 21

 

4、新建类库GaoJie.Lib,新建文件夹lib,里面有如下文件Iesi.Collections.dll、

NHibernate.dll、nunit.framework.dll

5、新建类库GaoJie.Dao,添加StudentDao类和IStudentDao接口。

StudentDao代码如下:

View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using GaoJie.Domain.Domain; 6 using NHibernate; 7 using NHibernate.Cfg; 8 using NHibernate.Linq; 9 10 namespace GaoJie.Dao11 {12     public class StudentDao:IStudentDao13     {14         #region fields15         private ISession session;16         private ISessionFactory sessionFactory;17         #endregion18 19         #region Constructor20         public StudentDao()21         {22             var cfg = new Configuration().Configure("Config/hibernate.cfg.xml");23 24             using (sessionFactory = cfg.BuildSessionFactory())25             {26             }            27         }28         #endregion29 30         #region Method31 32 33         public object Save(Student student)34         {35             using (session = sessionFactory.OpenSession())36             {37                 var id=session.Save(student);38                 session.Flush();39                 return id;40             }41         }42 43         public void Update(Student student)44         {45             using (session = sessionFactory.OpenSession())46             {47                 session.Update(student);48                 session.Flush();49             }               50         }51 52         public void Delete(Student student)53         {54             using (session = sessionFactory.OpenSession())55             {56                 session.Delete(student);57                 session.Flush();58             }59         }60 61         public Student Get(object id)62         {63             using (session = sessionFactory.OpenSession())64             {65                 return session.Get
(id);66 }67 }68 69 public IList
GetAll()70 {71 using (session=sessionFactory.OpenSession())72 {73 return session.Query
().ToList();74 }75 }76 77 private bool IsSessionOpen()78 {79 bool isSessionOpen = false;80 81 if (!session.IsOpen)82 {83 session = sessionFactory.OpenSession();84 isSessionOpen = true;85 }86 87 return isSessionOpen;88 }89 #endregion90 }91 }

 

IStudentDao代码如下:

View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using GaoJie.Domain.Domain; 6  7 namespace GaoJie.Dao 8 { 9     public interface IStudentDao10     {11         object Save(Student student);12 13         void Update(Student student);14 15         void Delete(Student student);16 17         Student Get(object id);18 19         IList
GetAll();20 }21 }

 

编写hibernate.cfg.xml:该文件在GaoJie.Dao类库下的Config文件夹下,设置该文件的属性,如图所示:

代码如下:

View Code
1 
2 3
4 5
6
NHibernate.Driver.SqlClientDriver
7
8 Server=(local);initial catalog=NHibernateDemo;Integrated Security=SSPI 9
10
10
11
true
12
NHibernate.Dialect.MsSql2008Dialect
13
60
14
update
15
true 1, false 0, yes 'Y', no 'N'
16
17
18 19

 

6、新建类库GaoJie.Test,添加引用

Iesi.Collections.dll、

NHibernate.dll、nunit.framework.dll

新建StudentTest类,编写代码:

View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using GaoJie.Domain.Domain; 6 using NHibernate; 7 using NHibernate.Cfg; 8 using NUnit.Framework; 9 using GaoJie.Dao;10 using GaoJie.Domain;11 12 13 namespace GaoJie.Test14 {15     [TestFixture]16     public class StudentTest17     {18         private IStudentDao studentDao; 19 20         [SetUp]21         public void Init()22         {23             studentDao=new StudentDao();24         }25 26         [Test]27         public void Save()28         {29             Student student=new Student()30                                 {31                                     Id =Guid.NewGuid(),32                                     Name = "张三",33                                     Age = 2234                                 };35 36             var id = studentDao.Save(student);37 38             Assert.NotNull(id);39         }40 41         [Test]42         public void Update()43         {44             int expected = 0;45             var student = studentDao.GetAll().FirstOrDefault();46             47             Assert.NotNull(student);48 49             student.Age = 43;50             expected = student.Age;51 52             studentDao.Update(student);53 54             Assert.AreEqual(expected,student.Age);55         }56 57         [Test]58         public void Delete()59         {60             var student = studentDao.GetAll().FirstOrDefault();61             var id = student.Id;62             Assert.NotNull(student);63 64             studentDao.Delete(student);65             Assert.Null(studentDao.Get(id));66         }67 68         [Test]69         public void Get()70         {71             var student = studentDao.GetAll().FirstOrDefault();72             Assert.NotNull(student);73 74             student=studentDao.Get(student.Id);75             Assert.NotNull(student);76         }77 78         [Test]79         public void GetAll()80         {81             var student = studentDao.GetAll().Count;82             Assert.NotNull(student>0);83         }84     }85 }

 

测试结果如下:

转载于:https://www.cnblogs.com/GaoHuhu/archive/2012/07/02/2573785.html

你可能感兴趣的文章
MYSQL数据库引擎 MYISAM和 INNODB区别
查看>>
设计模式之原型模式
查看>>
BootStrap常用组件及响应式开发
查看>>
TS学习之for..of
查看>>
OpenGL是什么?
查看>>
Oracle - 数据库巡检脚本
查看>>
Oracle 11g数据库详细安装步骤图解
查看>>
机器学习之特征选择---特征选择算法
查看>>
嵌入式开发之hisilicon---hi3536 处理器简介
查看>>
目标跟踪之模板匹配---简单的模板匹配
查看>>
css美化网页元素
查看>>
histogram
查看>>
51单片机点亮双向流水灯
查看>>
字符串前面+r
查看>>
Classic Binary Search
查看>>
Ubuntu 查看文件以及磁盘空间大小管理
查看>>
ExtJS与jQuery的一点细节上的对比
查看>>
Struts2源码浅析-初始化
查看>>
nginx安装
查看>>
angularjs 利用filter进行表单查询及分页查询
查看>>