在使用 mybatis.net 的時候,必然會涉及到命令參數問題。
1. 沒有參數的 SQL
最為簡單的就是沒有參數的命令了。比如下面查詢所有的產品,沒有使用任何參數。
<select id="GetAllProducts" resultMap="GetAllProductsResult"> <![CDATA[ SELECT productid, productname FROM [Production].Products ]]> </select>
那么,在代碼中可以直接傳遞一個 null,表示沒有參數。
public IList<EntityModel.Product> GetProductList() { IList<EntityModel.Product> productList = Mapper.QueryForList<EntityModel.Product>("GetAllProducts", null);
return productList; }
2. 只有一個參數的 SQL
如果你的語句只有一個參數,例如,通過產品的 Id 查詢特定的產品信息。這里使用的 SQL 語句定義如下。
<!-- 如果使用 resultClass,主要注意返回字段名稱的大小寫也需要與類中定義的屬性匹配 --> <select id="GetProductById" parameterClass="int" resultClass="Product" > <![CDATA[ select ProductId , ProductName from [Production].Products where productid = #value# ]]> </select>
此時,唯一的參數可以使用 #value# 表示,我們就不用挖空心思專門為它起一個名字了。
在代碼中,直接傳遞對應的參數即可,這個唯一的參數就對應 SQL 中定義的 #value# 。
public EntityModel.Product GetProductById(int id) { EntityModel.Product product = Mapper.QueryForObject<EntityModel.Product>("GetProductById", id);
return product; }
在上面的例子中,id 的值就傳遞給了 #value# 。
3. 多個參數問題
多個參數的話,就會涉及到區分參數的問題了。我們通常使用名值對來表示參數。
又有兩種方式,一種使用對象的屬性方式,一種使用字典方式。
先看我們熟悉的對象方式。可以定義一個表示參數的對象,對象的屬性名稱分別表示各個參數的名稱,對象的值就表示參數的值。
namespace EntityModel { public class Shipper { public int ShipperId { set; get; } public string ComapanyName { set; get; } public string Phone { set; get; } } }
然后,需要在映射文件中定義需要使用的類型。
<alias> <typeAlias alias="Shipper" type="EntityModel.Shipper, EntityModel"/> </alias>
隨后,用在 SQL 語句中。注意每個參數使用 #參數名# 進行表示,這里的名稱來自類中定義的屬性名稱,因此,一定要注意字母的大小寫問題。
insert 元素的 parameterClass 表示使用實體類來表示參數,這里的 Shipper 來自剛剛定義的類型別名。
<statements> <insert id="insertShipperByModel" parameterClass="Shipper" > <selectKey property="ShipperId" type="post" resultClass="int"> ${selectKey} </selectKey> <![CDATA[ insert into Sales.Shippers ( companyname, phone) values ( #ComapanyName#, #Phone# ); ]]> </insert> </statements>
語句開始中的 selectKey 用來在執行插入語句之后,獲取剛剛生成的標識值。這里使用到的 selectKey 定義在 SqlMap.config 的屬性中。
<properties> <property key="selectKey" value="select @@IDENTITY as value" />
好了,最后,我們可以在代碼中使用實體來傳遞參數了。數據訪問對象中的插入方法如下所示。
public int InsertShipper(EntityModel.Shipper model) { int result = (int) this.Mapper.Insert("insertShipperByModel", model); return result; }
第二種方式,需要使用字典來完成。
我們將所有的參數都定義為字典中的鍵值對。這里使用了字典的接口 IDictionary,注意鍵使用 string 類型來表示參數的名稱,而值使用了 object 類型。
public int InsertShipper(string companyName, string phone) { IDictionary<string, object> parameter = new Dictionary<string, object>(); parameter.Add("comapanyname", companyName); parameter.Add("phone", phone); int id = (int) this.Mapper.Insert("insertShipperByParameter", parameter); return id; }
在映射文件中,需要將 parameterClass 設置為字典接口 System.Collections.IDictionary。而 SQL 語句中則使用字典中的鍵值。還是要注意大小寫問題。這里同樣也是區分大小寫的。
<insert id="insertShipperByParameter" parameterClass="System.Collections.IDictionary"> <selectKey property="ShipperId" type="post" resultClass="int"> ${selectKey} </selectKey> <![CDATA[ insert into Sales.Shippers ( companyname, phone) values ( #comapanyname#, #phone# ); ]]> </insert>
4. 生成的標識列問題
在 mybatis.net 中使用 select @@IDENTITY as value 來獲取剛剛生成的標識列,你可能更想使用 select scope_identity() 來獲取。直接使用是不行的。
可以在映射文件中,將插入語句定義為一個普通的查詢語句,如下所示:
<select id="insertShipperByParameter" parameterClass="System.Collections.IDictionary"> <![CDATA[ insert into Sales.Shippers ( companyname, phone) values ( #comapanyname#, #phone# ); select cast( scope_identity() as int) value ]]> </select>
這里還使用了 cast 將 scope_identity() 轉化為整數,因為這個函數的類型實際上是 numbric ,並不是真正的整數類型。
在數據訪問對象中,則使用普通的查詢來調用這個 SQL 語句。
public int InsertShipper(string companyName, string phone) { IDictionary<string, object> parameter = new Dictionary<string, object>(); parameter.Add("comapanyname", companyName); parameter.Add("phone", phone); int id = this.Mapper.QueryForObject<int>("insertShipperByParameter", parameter); return id; }
這樣,就可以直接獲取生成的標識。