[NHibernate]預存程序的使用(三)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   sp   

目錄

寫在前面

文檔與系列文章

查詢

總結

寫在前面

前面的文章介紹了在nhibernate中使用預存程序進行增刪改的操作,當然查詢也是可以的,在nhibernate中也可以執行任意的預存程序。本篇文章將介紹如何使用查詢的預存程序的方式。

文檔與系列文章

[Nhibernate]體繫結構

[NHibernate]ISessionFactory配置

[NHibernate]持久化類(Persistent Classes)

[NHibernate]O/R Mapping基礎

[NHibernate]集合類(Collections)映射 

[NHibernate]關聯映射

[NHibernate]Parent/Child

[NHibernate]緩衝(NHibernate.Caches)

[NHibernate]NHibernate.Tool.hbm2net

[NHibernate]Nullables

[NHibernate]Nhibernate如何映射sqlserver中image欄位

[NHibernate]基本配置與測試 

[NHibernate]HQL查詢 

[NHibernate]條件查詢Criteria Query

[NHibernate]增刪改操作

[NHibernate]事務

[NHibernate]並發控制

[NHibernate]組件之依賴對象

[NHibernate]一對多關聯性(串聯刪除,級聯添加)

[NHibernate]一對多關聯性(關聯查詢)

[NHibernate]多對多關係(關聯查詢)

[NHibernate]消極式載入

[NHibernate]立即載入

[NHibernate]視圖處理

[NHibernate]N+1 Select查詢問題分析

[NHibernate]預存程序的使用(一)

[NHibernate]預存程序的使用(二)

查詢

使用節點<sql-query>根據使用者id進行查詢

添加預存程序

create proc [dbo].[ps_Search]  @CustomerID uniqueidentifierasbegin  select * from TB_Customer  where CustomerID=@CustomerID  end 

在對應檔中使用<sql-query>並定義<sql-query>查詢的名稱

1   <!--需要和class節點同一層級-->2   <sql-query name="ps_Search" >3    <return class="Wolfy.Shop.Domain.Entities.Customer,Wolfy.Shop.Domain" />4     exec ps_Search :CustomerID5   </sql-query>

測試

在資料訪問層中,使用ISession介面提供的GetNamedQuery方法來調用帶命名的預存程序,並傳遞一個整形參數。代碼如下:

 1         /// <summary> 2         /// 使用預存程序,進行查詢 3         /// </summary> 4         /// <returns></returns> 5         public IList<Customer> SearchCustomerByIDUsingProc(Guid customerID) 6         { 7             ISession session = NHibernateHelper.GetSession(); 8             //參數為對應檔中為<sql-query>節點指定的name 9 10             return session.GetNamedQuery("ps_Search")11                 .SetGuid("CustomerID", customerID)12                 .List<Customer>();13         }

結果

產生的sql語句

1 exec sp_executesql N‘exec ps_Search @p0‘,N‘@p0 uniqueidentifier‘,@p0=‘DDF63750-3307-461B-B96A-7FF356540CB8‘

如果就想返回一個實體的一部分屬性怎麼辦?
修改預存程序,只選擇需要的欄位

1 ALTER proc [dbo].[ps_Search]2   @CustomerID uniqueidentifier3 as4 begin5   select  CustomerName from TB_Customer6   where CustomerID=@CustomerID7   end 

修改對應檔

  <!--需要和class節點同一層級-->  <sql-query name="ps_Search" >   <!--<return class="Wolfy.Shop.Domain.Entities.Customer,Wolfy.Shop.Domain" />-->    <return-scalar column="CustomerName" type="String"/>    exec ps_Search :CustomerID  </sql-query>

修改方法

 1         /// <summary> 2         /// 使用預存程序,進行查詢 3         /// </summary> 4         /// <returns></returns> 5         public string SearchCustomerNameUsingProc(Guid customerID) 6         { 7             ISession session = NHibernateHelper.GetSession(); 8             //參數為對應檔中為<sql-query>節點指定的name 9             return session.GetNamedQuery("ps_Search")10                 .SetGuid("CustomerID", customerID).UniqueResult().ToString();11         }

結果

因為執行的是預存程序,sql語句與上面相同(在原來的預存程序基礎上修改的)。

最常見的bug

遇到這樣的異常,可以忽視它,我找了很久也沒找到解決方案,然後就直接.List<Customer>()將結果返回了。雖然得到結果了總有那麼點不太滿意(完美主義者)。

總結

這篇文章介紹了nhibernate中如何使用查詢預存程序的方法及需注意的地方,比如對應檔中<sql-query>節點需和<class>同層級才行。到這裡nhibernate中使用預存程序增刪改查就算結束了,說個題外話,使用orm為了少寫sql,所以在實際項目中使用nhibernate的預存程序比較少見。就算用也沒有直接在對應檔中添加<sql-**>的方式。因為你知道,不一定別人就知道,所以造成一定的維護難度,不過知道這種高大上的寫法,遇到的時候知道咋回事就ok了。

參考文章:http://www.cnblogs.com/lyj/archive/2008/11/07/1328782.html

[NHibernate]預存程序的使用(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.