在項目開發過程中,查詢佔了很大的一個比重,一個架構的好壞也很多程度上取決於查詢的靈活性和效率。
在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"
resultClass="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>
- 有時我們需要查看ibatisnet中statement產生的實際sql語句,
- 可以通過下面的方式取得:
public string getsql(string statementname, object paramobject)<br /> {<br /> IMappedStatement statement = mapper.GetMappedStatement(statementname);<br /> if (!mapper.IsSessionStarted)<br /> {<br /> mapper.OpenConnection();<br /> }<br /> RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramobject, mapper.LocalSession);<br /> return scope.PreparedStatement.PreparedSql;<br /> }