天天看點

IBatis.Net如何支援多個資料庫

原文: http://www.maplye.com:8081/post/114/

在Ibatis.net的幫助文檔中有介紹多資料庫支援,但是沒有寫全代碼,後來檢視其源碼,并結合幫助文檔,找到了解決方法,其實道理就是另行實作一個Mapper.如AnthorMapper:

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

Apache Notice #region Apache Notice    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

#endregion     

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

using  IBatisNet.Common.Utilities;    

IBatis.Net如何支援多個資料庫

using  IBatisNet.DataMapper;    

IBatis.Net如何支援多個資料庫

using  IBatisNet.DataMapper.Configuration;    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

namespace  IBatisNet.DataMapper    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

    /// <summary>    

IBatis.Net如何支援多個資料庫

    /// A singleton class to access the default SqlMapper defined by the SqlMap.Config    

IBatis.Net如何支援多個資料庫

    /// </summary>    

IBatis.Net如何支援多個資料庫

    public sealed class AnthorMapper    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{   

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

        Fields#region Fields    

IBatis.Net如何支援多個資料庫

        private static volatile ISqlMapper _mapper = null;   

IBatis.Net如何支援多個資料庫

        #endregion    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

        /// <summary>    

IBatis.Net如何支援多個資料庫

        ///     

IBatis.Net如何支援多個資料庫

        /// </summary>    

IBatis.Net如何支援多個資料庫

        /// <param name="obj"></param>    

IBatis.Net如何支援多個資料庫

        public static void Configure (object obj)    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫

            _mapper = null;    

IBatis.Net如何支援多個資料庫

        }    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

        /// <summary>    

IBatis.Net如何支援多個資料庫

        /// Init the 'default' SqlMapper defined by the SqlMap.Config file.    

IBatis.Net如何支援多個資料庫

        /// </summary>    

IBatis.Net如何支援多個資料庫

        public static void InitMapper()    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫

            ConfigureHandler handler = new ConfigureHandler (Configure);    

IBatis.Net如何支援多個資料庫

            DomSqlMapBuilder builder = new DomSqlMapBuilder();    

IBatis.Net如何支援多個資料庫

            _mapper = builder.ConfigureAndWatch ("AnthorMap.config",handler);      }    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

        /// <summary>    

IBatis.Net如何支援多個資料庫

        /// Get the instance of the SqlMapper defined by the SqlMap.Config file.    

IBatis.Net如何支援多個資料庫

        /// </summary>    

IBatis.Net如何支援多個資料庫

        /// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>    

IBatis.Net如何支援多個資料庫

        public static ISqlMapper Instance()    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫

            if (_mapper == null)    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫

                lock (typeof (SqlMapper))    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫

                    if (_mapper == null) // double-check    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{       

IBatis.Net如何支援多個資料庫

                        InitMapper();    

IBatis.Net如何支援多個資料庫

                    }    

IBatis.Net如何支援多個資料庫

                }    

IBatis.Net如何支援多個資料庫

            }    

IBatis.Net如何支援多個資料庫

            return _mapper;    

IBatis.Net如何支援多個資料庫

        }    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

        /// <summary>    

IBatis.Net如何支援多個資料庫

        /// Get the instance of the SqlMapper defined by the SqlMap.Config file. (Convenience form of Instance method.)    

IBatis.Net如何支援多個資料庫

        /// </summary>    

IBatis.Net如何支援多個資料庫

        /// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>    

IBatis.Net如何支援多個資料庫

        public static ISqlMapper Get()    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫

            return Instance();    

IBatis.Net如何支援多個資料庫

        }    

IBatis.Net如何支援多個資料庫

    }    

IBatis.Net如何支援多個資料庫

}     以上代碼隻是修改了IBatis.net中的Mapper的代碼,将_mapper = builder.ConfigureAndWatch (handler);修改為_mapper = builder.ConfigureAndWatch ("AnthorMap.config",handler),就是根據另一個AnthorMap.config檔案來生成SqlMapper。

AnthorMap.config和預設的SqlMap.config一樣,隻是根據你的資料不同設定不同而已,測試AnthorMap.config如下如下:

IBatis.Net如何支援多個資料庫

<? xml version="1.0" encoding="utf-8" ?>    

IBatis.Net如何支援多個資料庫

< sqlMapConfig     

IBatis.Net如何支援多個資料庫

   xmlns ="http://ibatis.apache.org/dataMapper"      

IBatis.Net如何支援多個資料庫

  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

   < settings >    

IBatis.Net如何支援多個資料庫

         < setting  useStatementNamespaces ="true" />    

IBatis.Net如何支援多個資料庫

     </ settings >    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

   < providers  resource ="ServerConfig/providers.config" />    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

   <!--  Database connection information  -->    

IBatis.Net如何支援多個資料庫

   < database >    

IBatis.Net如何支援多個資料庫

     < provider  name ="sqlServer2.0" />    

IBatis.Net如何支援多個資料庫

     < dataSource  name ="CrmSystem"  connectionString ="server=.;database=TestDB;uid=sa;pwd=" />    

IBatis.Net如何支援多個資料庫

   </ database >    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

     < sqlMaps >    

IBatis.Net如何支援多個資料庫

     < sqlMap  embedded ="Test.Domain.Weather.xml,Test.Domain"   />    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

   </ sqlMaps >    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

</ sqlMapConfig >     接下來就可以使用AntherMapper來建立ISqlMapper了。如下:

IBatis.Net如何支援多個資料庫

public  IList < Weather >  GetWeather()    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

{    

IBatis.Net如何支援多個資料庫

     ISqlMapper map = AnthorMapper.Instance();    

IBatis.Net如何支援多個資料庫
IBatis.Net如何支援多個資料庫

     return map.QueryForList<Weather>("Weather.Select", null);    

IBatis.Net如何支援多個資料庫

}