<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Nhibernate學習之起步篇-1

      1. 學習目的
      學習Nhibernate基礎知識。掌握Nhibernate的配置方法,實現對單表的簡單操作,如:創建表,查詢,添加,刪除,修改。
      2. 開發環境+前期準備
      開發環境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
      前期準備: Nhibernate框架,我用的目前最新版NHibernate-1.2.0.CR1, 下載地址:        http://downloads.sourceforge.net/nhibernate/NHibernate-1.2.0.CR1.msi?modtime=1172161735&big_mirror=0
      3. 開發步驟:
      1).雙擊下載下來的NHibernate-1.2.0.CR1.msi,將其安裝到某個目錄,我的目錄為: E:\download project\orm\nhibernate.,打開該目錄,即可以看到bin,doc,src三個子目錄,分別為Realse好的dll或者exe目錄,文檔說明目錄,和源程序目錄.
      2).打開visual studio 2005,創建類庫項目NhibernateSample1
      3).在解決方案管理其中,右鍵點擊引用-添加引用,在選項卡種選擇瀏覽,設定查找范圍為:E:\download project\orm\nhibernate\bin,添加對Nhibernate.dll,log4net.dll, Iesi.Collections, HashCodeProvider四個dll的引用.
      4).打開SQL Server Management Studio,創建數據庫nhibernate。
      5).在解決方案管理器中添加hibernate.cfg.xml文件。將下面代碼粘貼到此文件: 

      <?xml version="1.0" encoding="utf-8" ?>
      <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
          
      <session-factory name="NHibernate.Test">
              
      <!-- properties -->
              
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
              
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
              
      <property name="connection.connection_string">Server=127.0.0.1;initial catalog=nhibernate;uid=sa;pwd=123;</property>
              
      <property name="show_sql">false</property>
              
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
              
      <property name="use_outer_join">true</property>
              
      <!-- mapping files -->
              
      <mapping assembly="NhibernateSample1" />
          
      </session-factory>    
      </hibernate-configuration>

       該文件是Nhibernate的配置文件,其中connection.connection_string為數據庫連接字符串,Dialect項因為我用的是SQL2005,所以為:MsSql2005Dialect注意:<mapping assembly=”NhibernateSample1”/>表示映射NhibernateSample1程序集下的所有類,所以以后不要需要Configuration.AddClass(..)了;

      6).添加類文件:User.cs,添加代碼

      using System;
      using System.Collections.Generic;
      using System.Text;

      namespace NhibernateSample1
      {
          
      public class User
          
      {
              
      private int _id;
              
      private string _name;
              
      private string _pwd;
              
      /// <summary>
              
      /// 編號
              
      /// </summary>

              public virtual int Id
              
      {
                  
      get
                  
      {
                      
      return _id;
                  }

                  
      set
                  
      {
                      _id 
      = value;
                  }

              }


              
      /// <summary>
              
      /// 名稱
              
      /// </summary>

              public virtual string Name
              
      {
                  
      get
                  
      {
                      
      return _name;
                  }

                  
      set
                  
      {
                      _name 
      = value;
                  }

              }


              
      /// <summary>
              
      /// 密碼
              
      /// </summary>

              public virtual string Pwd
              
      {
                  
      get
                  
      {
                      
      return _pwd;
                  }

                  
      set
                  
      {
                      _pwd 
      = value;
                  }

              }

          }

      }

       6).編寫User類的映射配置文件:User.hbm.xml

      <?xml version="1.0" encoding="utf-8" ?>
      <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
        
      <class name="NhibernateSample1.User,NhibernateSample1" table="Users" lazy="false">
          
      <id name="Id" column="Id" unsaved-value="0">
            
      <generator class="native" />
          
      </id>
          
      <property name="Name" column="Name" type="string" length="64" not-null="true" unique="true"></property>
          
      <property name="Pwd"  column="Pwd"  type="string" length="64" not-null="true"></property>
        
      </class>
      </hibernate-mapping>

        注意:該映射文件的屬性中的生成操作必須為:嵌入的資源.

      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 project\nhibernate study\simle 1\NHibernateStudy1\NhibernateSample1\hibernate.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 NhibernateSample1
      {
          
      public class UserFixure
          
      {
              
      private ISession session;
              
      public UserFixure()
              
      {
                  
              }

              
      /// <summary>
              
      /// 創建表
              
      /// </summary>

              public bool ExportTable()
              
      {
                  
      try
                  
      {
                      Configuration cfg 
      = new Configuration().Configure(@"E:\my project\nhibernate study\simle 1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml");
                      session 
      = NHibernateHelper.GetCurrentSession();
                      ITransaction transaction 
      = session.BeginTransaction();
                      
      new SchemaExport(cfg).Create(truetrue);
                      transaction.Commit();
                      
      return true;
                  }

                  
      catch(Exception ex)
                  
      {
                      
      throw ex; 
                  }

                  
      finally
                  
      {
                      session.Close();
                  }

              }

              
      /// <summary>
              
      /// 添加
              
      /// </summary>

              public int Add()
              
      {
                  
      try
                  
      {
                      User 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;
                  }

                  
      finally
                  
      {
                      session.Close();
                  }

              }

              
      /// <summary>
              
      /// 更新
              
      /// </summary>
              
      /// <param name="uid"></param>

              public bool Update(int uid)
              
      {
                  
      try
                  
      {
                      session 
      = 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;
                  }

                  
      finally
                  
      {
                      session.Close();
                  }

              }

              
      /// <summary>
              
      /// 刪除
              
      /// </summary>
              
      /// <param name="uid"></param>
              
      /// <returns></returns>

              public bool Delete(int uid)
              
      {
                  
      try
                  
      {
                      session 
      = 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;
                  }

                  
      finally
                  
      {
                      session.Close();
                  }

              }

              
      public System.Collections.IList Query()
              
      {
                  
      try
                  
      {
                      session 
      = 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;
                  }

                  
      finally
                  
      {
                      session.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
      {
          
      /// <summary>
          
      /// UnitTest1 的摘要說明
          
      /// </summary>

          [TestClass]
          
      public class UnitTest1
          
      {
              
      public UnitTest1()
              
      {
                  
      //
                  
      // TODO: 在此處添加構造函數邏輯
                  
      //
              }


              
      其他測試屬性

              
      int uid;
              [TestMethod]
              
      public void TestMethod1()
              
      {
                  UserFixure userFixure 
      = new UserFixure();
                  Assert.IsTrue(userFixure.ExportTable());            
              }

              [TestMethod]
              
      public void TestMethod2()
              
      {
                  UserFixure userFixure 
      = new UserFixure();
                  uid 
      = userFixure.Add();
                  Assert.IsTrue(uid
      >0);
              }

              [TestMethod]
              
      public void TestMethod3()
              
      {
                  UserFixure userFixure 
      = new UserFixure();
                  Assert.IsTrue(userFixure.Update(uid));
              }

              [TestMethod]
              
      public void TestMethod4()
              
      {
                  UserFixure userFixure 
      = new UserFixure();
                  Assert.IsTrue(userFixure.Delete(uid));
              }

              [TestMethod]
              
      public void TestMethod5()
              
      {
                  UserFixure userFixure 
      = new UserFixure();
                  Assert.IsTrue(userFixure.Query().Count
      >0);
              }

          }

      }


      11)在菜單-測試-加載元數據文件 選擇NHibernateStudy1.vsmdi,然后按順序執行TestMethod1-TestMethod5,全部成功!
      4.總結
       通過使用Nhibernate,基本上可以使開發人員不在接觸繁瑣的數據庫表和數據庫操作代碼,您唯一需要關心的就是如何設計好類,讓這些類滿足您的業務需求。從擴展性來說Nhinernate具有非常好的擴展性。與代碼生成比較,Nhibernate更改數據表結構對代碼的影響要遠遠小于代碼生成。
      如果您想下載Demo:/Files/jillzhang/simle.rar
      posted @ 2007-03-21 22:51  Robin Zhang  閱讀(23932)  評論(58)    收藏  舉報
      主站蜘蛛池模板: 亚洲欧美日韩国产精品专区| 免费人成无码大片在线观看| 中文字幕日韩精品东京热| 国产日韩成人内射视频| 国产一区二区三区综合视频| 无码日韩精品一区二区三区免费 | 国产精品亚洲二区在线播放| 一区二区三区精品不卡| 日本免费人成视频在线观看| 一区二区三区午夜无码视频| 国产麻豆精品一区一区三区| 国产亚洲精品久久久久婷婷瑜伽| 中文字幕免费不卡二区| 成 人色 网 站 欧美大片在线观看| 国产乱老熟女乱老熟女视频| 国产成人一区二区不卡| 人妻一区二区三区三区| 日本乱码在线看亚洲乱码| 久久无码高潮喷水| 色偷偷亚洲女人天堂观看| 亚洲精品~无码抽插| 亚洲国产美国产综合一区| 在线高清免费不卡全码| 波多野结衣的av一区二区三区| 人人妻人人妻人人片色av| 国产热A欧美热A在线视频| 丰满高跟丝袜老熟女久久| 亚洲精品二区在线播放| 黄色A级国产免费大片视频| 国产精品永久久久久久久久久| 日本国产精品第一页久久| 人人做人人澡人人人爽| www插插插无码免费视频网站| 欧美人成精品网站播放| 国产毛片基地| 99e久热只有精品8在线直播| 国产综合色在线精品| 午夜欧美精品久久久久久久 | 97国产成人无码精品久久久| 亚洲日韩av无码一区二区三区人 | 亚洲成a人片在线观看中文|