整个项目的结构图如下:
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 34 5 6 7 20 218 10 119 12 14 1513 16 18 1917
4、新建类库GaoJie.Lib,新建文件夹lib,里面有如下文件Iesi.Collections.dll、
NHibernate.dll、nunit.framework.dll5、新建类库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 IListGetAll();20 }21 }
编写hibernate.cfg.xml:该文件在GaoJie.Dao类库下的Config文件夹下,设置该文件的属性,如图所示:
代码如下:
View Code
1 2 34 5 6 18 19NHibernate.Driver.SqlClientDriver 78 Server=(local);initial catalog=NHibernateDemo;Integrated Security=SSPI 9 1010 11true 12NHibernate.Dialect.MsSql2008Dialect 1360 14update 15true 1, false 0, yes 'Y', no 'N' 1617
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 }
测试结果如下: