1、添加引用:
2、添加config文件:providers.config, SqlMap.config
providers.config 裏提供數據庫的相關信息,如下:
我這裏只使用了SQLServer2000。
SqlMap.config裏添加數據庫的鏈接字符串和使用的xml文件。
3、添加XML文件
XML文件裏是使用到的SQL語句和類到數據庫的反射。如上在Sql.Map文件中我使用到了兩個.xml文件。
StandardTech.xml文件如下:
此文件中只有一個select語句“select SID,StandardNo,StandardName,Material,Type from V_JjCardIndex where type=#type# order by StandardNo”
如果要添加其他的sql語句,直接在<statements></statements>中間加即可。
在<select></select>裏有一個id,便於應用程序使用對應的sql語句,resultMap跟前面的<resultMap>對應,parameterClass說明參數的類型。
resultMap將數據庫裏的表或視圖跟創建的某個類對應,如此處就對應到class=“StandardTech”,property是StandardTech類的屬性,column是對應的數據庫裏的字段。
4、.cs文件應用
如要將上面 StandardTech.xml文件裏的select語句獲取的結果綁定到一個GridView,代碼如下:
Mapper在添加了 using IBatisNet.DataMapper之後就有了。QueryForList的兩個參數:
-- “StandardTech.SelectAllStandardTech”是指StandardTech.xml文件裏的信息,StandardTech是<sqlMap>裏的namespace,SelectAllStandardTech是<select>的id.
-- 0 是傳進到SelectAllStandardTech的參數.