您好,登錄后才能下訂單哦!
關(guān)于NHibername 存儲過程的文章不多,而且網(wǎng)上搜索基本上示例程序也就那幾個,尤其是對于返回記錄集的存儲過程。本人根據(jù)研究總結(jié)此文,該文是針對MySql數(shù)據(jù)庫的,其他數(shù)據(jù)庫部分細(xì)節(jié)略有不同。
1,創(chuàng)建存儲過程:
DELIMITER $$ DROP PROCEDURE IF EXISTS `test_get_prices` $$ CREATE DEFINER=`admin`@`%` PROCEDURE `test_get_prices`() BEGIN select a.id as id,a.xibie as xb,b.checi as cc,a.OStation as os,a.DStation as ds from noshow_data as a, phone_book_data as b where a.checi=b.checi and a.date=b.go_date; END $$ DELIMITER ;
這是一個返回記錄集的存儲過程,可能跨幾個表,并且只取部分表的部分字段,還有可能是sum等根據(jù)其它值計(jì)算出的結(jié)果組成的臨時字段。
值得注意的是,如果想使用NHibernate對該存儲過程結(jié)果集進(jìn)行映射,就必須保證返回的結(jié)果集有主鍵,這是因?yàn)镹Hibernate的mapping文件必須要有主鍵的定義項(xiàng)(本人一直未試出無主鍵或者mapping文件為結(jié)果集自己生成主鍵的方法)。
2,編寫Model類:
public class ProcRequestPrice { public virtual int Id { get; set; } public virtual string Checi { get; set; } public virtual string Xibie { get; set; } public virtual string OStation { get; set; } public virtual string DStation { get; set; } }
Model類的字段是根據(jù)存儲過程返回的結(jié)果集確定的。
3,編寫NHibernate Mapping文件:
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly="Data.MySql" namespace="Data.MySql.models" xmlns="urn:nhibernate-mapping-2.2"> <class name="ProcRequestPrice" mutable="false"> <id name="Id" type="System.Int32"> <generator class="increment" /> </id> <property name="Checi" column="cc" type="System.String" /> <property name="Xibie" column="xb" type="System.String" /> <property name="OStation" column="os" type="System.String" /> <property name="DStation" column="ds" type="System.String" /> </class> <sql-query name="GetRequestPrices" callable="true"> <return alias="requestPrice" class="ProcRequestPrice"> </return> call test_get_prices() </sql-query> </hibernate-mapping>
其中mutable="false",是指該對象只讀不可變。GetRequestPrices是用于DAL中調(diào)用的標(biāo)識。這里沒有指定映射的表名,因?yàn)橛成涞牟皇菍?shí)體表,是存儲過程返回的結(jié)果集。
MySql下為call test_get_prices(), SqlServe下為exec test_get_prices(),帶參數(shù)的請參考文章:
http://www.cnblogs.com/lyj/archive/2008/11/03/1325291.html
http://www.cnblogs.com/lyj/archive/2008/11/06/1328240.html
http://www.cnblogs.com/lyj/archive/2008/11/07/1328782.html
記得設(shè)置Mapping文件為嵌入的資源。
4,調(diào)用及測試程序的編寫
本測試程序是基于NUnit編寫的:
[TestFixture] public class CustomerTest { private NHibernateHelper nhibernateHelper = new NHibernateHelper(); [Test] public void ProcedureTest() { try { IQuery query = nhibernateHelper.GetSession().GetNamedQuery("GetRequestPrices"); List<ProcRequestPrice> rs = query.List<ProcRequestPrice>() as List<ProcRequestPrice>; Console.Out.WriteLine("count:" + rs.Count); foreach (ProcRequestPrice rp in rs) { Console.Out.WriteLine("value:" + rp.Id + "," + rp.Checi + "," + rp.Xibie + "," + rp.OStation + "," + rp.DStation); } } catch (Exception e) { Console.Out.WriteLine(e.StackTrace.ToString()); } } }
測試通過,大功告成。
另外,目前有一個開源的NHibernate O/R Mapping生成工具:NHibernateMappingGenerator,不過還處于開發(fā)階段,bug很多,只能生成Model,Mapping文件,我在目前最新版本上修復(fù)了幾個Bug,并增加了生成DAL和IDAL,且生成工具能合并DAL中自定義的方法。等有空整體發(fā)上來。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。