zoukankan      html  css  js  c++  java
  • 我的第一个NHibernate示例

    整个项目的结构图如下:

    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     /// <summary>
     9     /// Student
    10     /// </summary>
    11     public class Student
    12     {
    13         /// <summary>
    14         /// Id
    15         /// </summary>
    16         public virtual Guid Id { set; get; }
    17 
    18         /// <summary>
    19         /// Name
    20         /// </summary>
    21         public virtual string Name { set; get; }
    22 
    23         /// <summary>
    24         /// Age
    25         /// </summary>
    26         public virtual int Age { set; get; }
    27     }
    28 }

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

    代码如下:

    View Code
     1 <?xml version="1.0" encoding="utf-8" ?>
     2 
     3 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="GaoJie.Domain" namespace="GaoJie.Domain.Domain">
     4 
     5   <class name="Student" table="T_Student" lazy="true">
     6 
     7     <id name="Id" column="ID" type="Guid">
     8       <generator class="assigned"/>
     9     </id>
    10 
    11     <property name="Name" type="string">
    12       <column name="Name" length="50"/>
    13     </property>
    14 
    15     <property name="Age" type="int">
    16       <column name="Age"/>
    17     </property>
    18 
    19   </class>
    20 
    21 </hibernate-mapping>

    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.Dao
    11 {
    12     public class StudentDao:IStudentDao
    13     {
    14         #region fields
    15         private ISession session;
    16         private ISessionFactory sessionFactory;
    17         #endregion
    18 
    19         #region Constructor
    20         public StudentDao()
    21         {
    22             var cfg = new Configuration().Configure("Config/hibernate.cfg.xml");
    23 
    24             using (sessionFactory = cfg.BuildSessionFactory())
    25             {
    26             }            
    27         }
    28         #endregion
    29 
    30         #region Method
    31 
    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<Student>(id);
    66             }
    67         }
    68 
    69         public IList<Student> GetAll()
    70         {
    71             using (session=sessionFactory.OpenSession())
    72             {
    73                 return session.Query<Student>().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         #endregion
    90     }
    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 IStudentDao
    10     {
    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<Student> GetAll();
    20     }
    21 }

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

    代码如下:

    View Code
     1 <?xml version="1.0" encoding="utf-8"?>
     2 
     3 <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
     4 
     5   <session-factory name="GaoJie.NHibernate.Domain">
     6     <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
     7     <property name="connection.connection_string">
     8       Server=(local);initial catalog=NHibernateDemo;Integrated Security=SSPI
     9     </property>
    10     <property name="adonet.batch_size">10</property>
    11     <property name="show_sql">true</property>
    12     <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    13     <property name="command_timeout">60</property>
    14     <property name="hbm2ddl.auto">update</property>
    15     <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    16     <mapping assembly="GaoJie.Domain"/>
    17   </session-factory>
    18 
    19 </hibernate-configuration>

    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.Test
    14 {
    15     [TestFixture]
    16     public class StudentTest
    17     {
    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 = 22
    34                                 };
    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 }

    测试结果如下:
    QQ截图20120702222340

  • 相关阅读:
    随机生成三十个二年级的加减乘除法的题目代码
    学习进度表_四周
    返回一个整数数组中最大子数组的和1
    学习进度表_三周
    四则运算4
    构建之法读书笔记2
    学习进度表_二周
    四则运算3
    四则运算2
    单元测试
  • 原文地址:https://www.cnblogs.com/GaoHuhu/p/2573785.html
Copyright © 2011-2022 走看看