在Ibatis.net的帮助文档中有介绍多数据库支持,但是没有写全代码,网上的资料也比较少,结合网上的资料,通过自己的测试,终于可以对多数据库支持,原理就是实现另一个Mapper。
dao.config文件实现:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <daoConfig xmlns="http://ibatis.apache.org/dataAccess" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 3 4 <providers resource="providers.config"/> 5 6 <context id="SqlMapDao" default="true"> 7 8 <!-- ==== Sql Server : SqlClient configuration ========= --> 9 <database> 10 <provider name="sqlServer2.0" /> 11 <dataSource name="SqlServer" connectionString="data source=(local);database=Top4Pos;user id=sa;password=123;" /> 12 </database> 13 14 <daoSessionHandler id="SqlMap"> 15 <property name="resource" value="SqlMap.config" /> 16 </daoSessionHandler> 17 18 <daoFactory> 19 <dao interface="Top4ever.Interface.Accounts.IEmployeeDao, Top4ever.Interface" 20 implementation="Top4ever.Persistence.Accounts.EmployeeSqlMapDao, Top4ever.Persistence" /> 21 ...... 22 </daoFactory> 23 24 </context> 25 26 <context id="TestSqlMapDao" default="false"> 27 28 <!-- ==== Sql Server : SqlClient configuration ========= --> 29 <database> 30 <provider name="sqlServer2.0" /> 31 <dataSource name="SqlServer" connectionString="data source=(local);database=Top4Pos130820;user id=sa;password=123;" /> 32 </database> 33 34 <daoSessionHandler id="SqlMap"> 35 <property name="resource" value="SqlMap.config" /> 36 </daoSessionHandler> 37 38 <daoFactory> 39 <dao interface="Top4ever.Interface.Accounts.IAccountDao, Top4ever.Interface" 40 implementation="Top4ever.Persistence.Accounts.AccountSqlMapDao, Top4ever.Persistence" /> 41 ...... 42 </daoFactory> 43 44 </context> 45 46 </daoConfig>
ServiceConfig.cs文件实现:
1 using System; 2 using System.Collections.Generic; 3 using IBatisNet.Common.Utilities; 4 using IBatisNet.DataAccess; 5 using IBatisNet.DataAccess.Configuration; 6 using IBatisNet.DataAccess.Interfaces; 7 8 namespace Top4ever.Service 9 { 10 /// <summary> 11 /// Summary description for ServiceConfig. 12 /// </summary> 13 public class ServiceConfig 14 { 15 private static readonly object _synRoot = new Object(); 16 private static ServiceConfig _instance; 17 18 private readonly IDictionary<string, IDaoManager> _daoManagerCollection = new Dictionary<string, IDaoManager>(); 19 20 /// <summary> 21 /// Remove public constructor. prevent instantiation. 22 /// </summary> 23 private ServiceConfig() { } 24 25 public static ServiceConfig GetInstance() 26 { 27 if (_instance == null) 28 { 29 lock (_synRoot) 30 { 31 if (_instance == null) 32 { 33 try 34 { 35 ConfigureHandler handler = new ConfigureHandler(ServiceConfig.Reset); 36 DomDaoManagerBuilder builder = new DomDaoManagerBuilder(); 37 builder.ConfigureAndWatch("dao.config", handler); 38 } 39 catch (Exception ex) 40 { 41 throw ex; 42 } 43 44 _instance = new ServiceConfig(); 45 // TODO:默认为sqlMapDao指定的Context, 要提供对多个Context的支持. 46 string contextName = "SqlMapDao"; 47 IDaoManager daoManager = IBatisNet.DataAccess.DaoManager.GetInstance(contextName); 48 if (!_instance._daoManagerCollection.ContainsKey(contextName)) 49 { 50 _instance._daoManagerCollection.Add(contextName, daoManager); 51 }56 contextName = "TestSqlMapDao"; 57 daoManager = IBatisNet.DataAccess.DaoManager.GetInstance(contextName); 58 if (!_instance._daoManagerCollection.ContainsKey(contextName)) 59 { 60 _instance._daoManagerCollection.Add(contextName, daoManager); 61 }66 } 67 } 68 } 69 return _instance; 70 } 71 72 73 /// <summary> 74 /// Reset the singleton 75 /// </summary> 76 /// <remarks> 77 /// Must verify ConfigureHandler signature. 78 /// </remarks> 79 /// <param name="obj"> 80 /// </param> 81 static public void Reset(object obj) 82 { 83 _instance = null; 84 } 85 86 public IDaoManager DaoManager 87 { 88 get 89 { 90 return _daoManagerCollection["SqlMapDao"]; 91 } 92 } 93 94 public IDaoManager GetDaoManager(string contextName) 95 { 96 return _daoManagerCollection[contextName]; 97 } 98 } 99 }