IBatis.net做用是把數據庫查詢與對象的屬性間創建映射關係。但它並非一個實體關係映射工具,僅用於幫助程序人員創建實體和SQL語句或者存儲過程間的映射。所以只能叫半自動OR/M工具。咱們的開票系統也用到了這個工具。sql
IBatis.net的配置:數據庫
1、引用幾個DLL,注意在數據層引用便可。apache
單獨使用映射的狀況下,只須要引用IBatisNet.DataMapper.dll就能夠了緩存
其中IBatisNet.Common.dll是必須的,Entities是項目本身的,不用管它app
2、完成對組件的添加後,還須要添加三個XML文檔ide
1 providers.config ----DataMapper根據這個肯定是什麼類型數據庫(放在數據層)工具
2 SqlMap.xml ----數據映射文檔,裏面包含了SQL語句(放在數據層)ui
3 SqlMap.config ----DataMapper的配置文檔,它詳細描述了工程中SqlMap.XML和providers.config文檔的位置,以及其餘配置項(必須放在Web跟目錄下)。spa
先看SqlMap.config(這個文件的做用主要是指定db鏈接串,告訴系統providers.config在哪? 以及db與entity的映射文件在哪?):.net
<?xml version="1.0" encoding="utf-8"?> <sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <settings> <setting useStatementNamespaces="true"/> //若是是true,那麼寫數據查詢時,查詢語句的名稱前要添加它的完整命名空間 <setting cacheModelsEnabled="true"/> //全局化配置DataMapper客戶可否啓用cache,默認是true <setting validateSqlMap="false"/> </settings> <providers embedded="ICSON.InvoicePrinter.SqlProviderImpl.Config.providers.config,ICSON.InvoicePrinter.SqlProviderImpl"/>
//指定providers.config的位置
<database> <provider name="sqlServer2.0" /> //若是使用默認的數據提供者,這句能夠不要,若是系統中使用多個數據庫,須要配置provider的內容 <dataSource name="SqlServer" connectionString="${ConnectionString}" /> //數據庫連接字符串,咱們的項目在IninMapper時,會給這個變量賦值 </database> <sqlMaps> //程序的數據映射文件的位置,若是有多個XML,就寫多行,若是比較多,也能夠當一個單獨的XML中去寫,好比<sqlMap resource=」Maps/All.XML」/>,而後在ALL.xml再添加數據映射文件,這樣就 實現了加載一組數據映射文件
<sqlMap resource="Maps/KeyValueEntity.xml"/> <sqlMap resource="Maps/InvoiceCompany.xml"/> <sqlMap resource="Maps/InvoiceTemplate.xml"/> <sqlMap resource="Maps/InvoiceDistribute.xml"/> <sqlMap resource="Maps/BlankInvoiceInventory.xml"/> <sqlMap resource="Maps/InvoiceMachine.xml" /> <sqlMap resource="Maps/Order.xml"/> <sqlMap resource="Maps/SoPrint.xml"/> <sqlMap resource="Maps/SoPrintItem.xml"/> <sqlMap resource="Maps/Invoice.xml"/> <sqlMap resource="Maps/Log.xml"/> <sqlMap resource="Maps/InvoiceUser.xml"/> <sqlMap resource="Maps/OrderOutStockMaster.xml"/> <sqlMap resource="Maps/Batch.xml"/> <sqlMap resource="Maps/OrderBatch.xml"/> </sqlMaps> </sqlMapConfig>
3、建立SqlMapper實例,這個是單例模式,文件名ProviderBase.cs,放在數據層中。
using System; using IBatisNet.Common.Utilities; using IBatisNet.DataMapper; using IBatisNet.DataMapper.Configuration; using ICSON.Utility; using System.Configuration; using System.Collections.Specialized; namespace ICSON.InvoicePrinter.SqlProviderImpl { /// <summary> /// 數據訪問配置類。 /// </summary> public class ProviderBase { private static volatile ISqlMapper _mapper; private ProviderBase() { } public static ISqlMapper Instance() { if (_mapper == null) { lock (typeof(SqlMapper)) { if (_mapper == null) // double-check { InitMapper(); } } } return _mapper; } /// <summary> /// Init the 'default' SqlMapper defined by the SqlMap.Config file. /// </summary> public static void InitMapper() { try { var handler = new ConfigureHandler(Configure); var builder = new DomSqlMapBuilder(); var connection = ConfigurationManager.ConnectionStrings[Config.DefaultConnectionName]; if (connection == null) throw new ConfigurationErrorsException("缺乏數據庫鏈接配置(" + Config.DefaultConnectionName + ")"); var properties = new NameValueCollection { {"ConnectionString",connection.ConnectionString} }; builder.Properties = properties; _mapper = builder.ConfigureAndWatch(Config.BasePath + "sqlmap.config", handler); } catch (Exception e) { Log.Error("sqlmap.config配置錯誤:"+e.ToString()); throw; } } public static void Configure(object obj) { _mapper = null; } public static ISqlMapper Get() { return Instance(); } } }
外界調用方法:
IList list = SqlMapper.Instance().QueryForObject<list>("UserInfo.GetCount", param);
由於是單例的,第一次調用時,DomSqlMapBuilder對象會經過查詢SqlMap.config來建立一個sqlMapper實例,之後調用就直接從緩存讀取了。
DomSqlMapBuilder.ConfigureAndWatch()方法負責監視配置文件的更新狀況,若是配置或者映射文件有更新,SqlMapper對象會從新載入並不重啓系統。
咱們的項目創建了一個SqlMap訪問層基類,文件名是 BaseSqlMapDao.cs
那麼這個基類完成什麼工做呢,見下一篇博客吧。