iBATIS.net常用的查詢方式淺析
在項目開發過程中,查詢占了很大的一個比重,一個框架的好壞也很多程度上取決于查詢的靈活性和效率。
在IBatis.Net中提供了方便的數據庫查詢方式。
在Dao代碼部分主要有兩種方式:
1、查詢結果為一個對象:
- ISqlMapper sqlMap = sqlMapDaoSession.SqlMap;
- return (Account) sqlMap.QueryForObject("GetAccountViaColumnName", accountID);
2、查詢結果為一個列表:
- ISqlMapper sqlMap = sqlMapDaoSession.SqlMap;
- return (ArrayList)sqlMap.QueryForList("GetAccountAsHashtableResultClass", 1);
這兩種方法同時都提供了面向泛型的重載方法。這兩個方法的第一個參數對應配置文件中的select id,第二個參數表示傳入查詢的條件
配置文件的寫法:
在IBatis.Net中提供了多種查詢配置的寫法,我這里列出幾種比較常用的方式:
1、獲得一張表的所有數據
- ﹤select id="GetAllAccountsAsHashMapViaResultMap"
- resultMap="account-hashtable-result"﹥
- select *
- from Accounts
- order by Account_ID
- ﹤/select﹥
這是最簡單的方式,其中resultMap是返回查詢結果的形式,需要另外配置:
- ﹤resultMap id="account-hashtable-result" class="Hashtable"﹥
- ﹤result property="Id" column="Account_ID"/﹥
- ﹤result property="FirstName" column="Account_FirstName"/﹥
- ﹤result property="LastName" column="Account_LastName"/﹥
- ﹤result property="EmailAddress" column="Account_Email"/﹥
- ﹤/resultMap﹥
表示:得到的結果的每一條記錄都映射成一個Hashtable,這個Hashtable中包含四個Key(Id,FirstName......)
2、根據條件查詢(簡單方式):
- ﹤select id="GetAccountViaColumnIndex"
- parameterClass="int"
- resultMap="indexed-account-result"﹥
- select
- Account_ID,
- Account_FirstName,
- Account_LastName,
- Account_Email
- from Accounts
- where Account_ID = #value#
- ﹤/select﹥
只有一個條件,傳入參數的類型是int型,拼寫sql時直接用 #value#就可以了
3、根據條件查詢(較復雜方式):
- ﹤select id="GetAccountsDynamic" resultMap="account-result" parameterClass="Hashtable" ﹥
- select top $MaximumAllowed$ * from Accounts
- ﹤dynamic prepend="where"﹥
- ﹤isParameterPresent﹥
- ﹤isNotEmpty prepend="and" property="FirstName" ﹥
- Account_FirstName LIKE '%$FirstName$%'
- ﹤/isNotEmpty﹥
- ﹤isNotEmpty prepend="and" property="LastName" ﹥
- Account_LastName LIKE '%$LastName$%'
- ﹤/isNotEmpty﹥
- ﹤isNotEmpty prepend="and" property="EmailAddress" ﹥
- Account_Email LIKE '%$EmailAddress$%'
- ﹤/isNotEmpty﹥
- ﹤/isParameterPresent﹥
- ﹤/dynamic﹥
- order by Account_LastName
- ﹤/select﹥
傳入參數是一個Hashtable,MaximumAllowed等表示的是Hashtable里的key值,用$$包含起來。
并且查詢時可以根據條件是否為空動態拼寫sql語句
PS:輸入參數同樣可以使用Account類,注意對應的鍵要和類中的屬性名一致(大小寫也要一樣)
4、多表查詢
多表查詢時返回參數有三種方式,一種是新建一個類,在這個類中包含這多個表的所有屬性,還有一種就是直接返回Hastable就可以了:
- ﹤select id="GetAccountAsHashtableResultClass"
- sultClass="HashMap"﹥
- select
- a.*,b.*
- from a,b
- where a.Account_ID = b.Account_ID
- ﹤/select﹥
PS:這里的HashMap實際上就是Hashtable
第三種方式是使用IBatis中的復雜屬性(感謝Anders Cui 的提醒)
比如現在有兩張表Account和Degree,使用Account_ID關聯,那么需要在原有的基礎上修改:
1、修改Account實體類,加入一個屬性:
- private Degree _degree;
- public Degree Degree
- {
- get
- {
- return _degree;
- }
- set
- {
- _degree = value;
- }
- }
這樣是一個1:1的關系,也可以加入IList DegreeList的屬性,這樣查詢的結果就是一個1:n的關系
2、修改配置文件:
在resultMaps節加入:
- ﹤resultMap id="comresult" class="Account" ﹥
- ﹤result property="Id" column="Account_ID"/﹥
- ﹤result property="FirstName" column="Account_FirstName"/﹥
- ﹤result property="LastName" column="Account_LastName"/﹥
- ﹤result property="EmailAddress" column="Account_Email" nullValue="no_email@provided.com"/﹥
- ﹤result property="Degree" column="Account_ID=Account_ID" select="degreeretrive" /﹥
- ﹤/resultMap﹥
對于Degree屬性,還可以加入lazyLoad=true 延遲加載,優化性能(也就是開始時并沒有實際查詢數據庫,當用到屬性Degree時,才實際的查詢相應的數據)
在statements節加入:
- ﹤statement id="degreeretrive"
- parameterClass="Hashtable"
- resultClass="Degree"﹥
- select *
- from Degree
- where Account_id = #Account_ID#
- ﹤/statement﹥
- ﹤select id="GetComTables"
- resultMap="comresult"﹥
- select *
- from Accounts
- order by Account_ID
- ﹤/select﹥
這樣可以正確的查詢出結果,符合OO,但是也有兩個小問題:
1、比較麻煩,不夠靈活
2、性能受影響:
這種方式其實和Hibernet比較類似了,查詢時首先執行
- select * from Accounts order by Account_ID
然后根據這條語句的結果,比如有100條記錄,那就要執行100次以下的語句:
- select * from Degree where Account_id = @param0
關于輸入輸出:
從上面可以看到輸入時可以使用:parameterClass和parameterMap,輸出時可以使用:resultClass和resultMap
對于resultMap和parameterMap我們需要另外進行配置(如上所示)
對于parameterClass和resultClass,如果是C#固有類型可以直接使用,如果是我們自定義類可以在SqlMap.config中先統一聲明一下:
- ﹤alias﹥
- ﹤typeAlias alias="Account" type="GSpring.Domain.Account"/﹥
- ﹤/alias﹥
iBATIS.net常用的查詢方式就向你介紹到這里,希望通過介紹能夠使你了解到iBATIS.net常用的查詢方式。
【編輯推薦】