IBatis.Net如何支持多个数据库

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

在Ibatis.net的帮助文档中有介绍多数据库支持,但是没有写全代码,后来查看其源码,并结合帮助文档,找到了解决方法,其实道理就是另行实现一个Mapper.如AnthorMapper:
Apache Notice #region Apache Notice    
/**//*****************************************************************************   
 * $Header: $   
 * $Revision: 513429 $   
 * $Date: 2007-03-02 02:32:25 +0800 (星期五, 02 三月 2007) $   
 *    
 * iBATIS.NET Data Mapper   
 * Copyright (C) 2004 - Gilles Bayon   
 *     
 *    
 * Licensed under the Apache License, Version 2.0 (the "License");   
 * you may not use this file except in compliance with the License.   
 * You may obtain a copy of the License at   
 *    
 *      
http://www.apache.org/licenses/LICENSE-2.0   
 *    
 * Unless required by applicable law or agreed to in writing, software   
 * distributed under the License is distributed on an "AS IS" BASIS,   
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
 * See the License for the specific language governing permissions and   
 * limitations under the License.   
 *    
 *******************************************************************************
*/
  
#endregion
    
   
using  IBatisNet.Common.Utilities;    
using  IBatisNet.DataMapper;    
using  IBatisNet.DataMapper.Configuration;    
   
namespace  IBatisNet.DataMapper    
{    
    
/**//// <summary>    
    
/// A singleton class to access the default SqlMapper defined by the SqlMap.Config    
    
/// </summary>    

    public sealed class AnthorMapper    
    
{   
        
Fields#region Fields    
        
private static volatile ISqlMapper _mapper = null;   
        
#endregion
    
   
        
/**//// <summary>    
        
///     
        
/// </summary>    
        
/// <param name="obj"></param>    

        public static void Configure (object obj)    
        
{    
            _mapper 
= null;    
        }
    
   
        
/**//// <summary>    
        
/// Init the 'default' SqlMapper defined by the SqlMap.Config file.    
        
/// </summary>    

        public static void InitMapper()    
        
{    
            ConfigureHandler handler 
= new ConfigureHandler (Configure);    
            DomSqlMapBuilder builder 
= new DomSqlMapBuilder();    
            _mapper 
= builder.ConfigureAndWatch ("AnthorMap.config",handler);      }
    
   
        
/**//// <summary>    
        
/// Get the instance of the SqlMapper defined by the SqlMap.Config file.    
        
/// </summary>    
        
/// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>    

        public static ISqlMapper Instance()    
        
{    
            
if (_mapper == null)    
            
{    
                
lock (typeof (SqlMapper))    
                
{    
                    
if (_mapper == null// double-check    
                    {       
                        InitMapper();    
                    }
    
                }
    
            }
    
            
return _mapper;    
        }
    
            
        
/**//// <summary>    
        
/// Get the instance of the SqlMapper defined by the SqlMap.Config file. (Convenience form of Instance method.)    
        
/// </summary>    
        
/// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>    

        public static ISqlMapper Get()    
        
{    
            
return Instance();    
        }
    
    }
    
}
   
以上代码只是修改了IBatis.net中的Mapper的代码,将_mapper = builder.ConfigureAndWatch (handler);修改为_mapper = builder.ConfigureAndWatch ("AnthorMap.config",handler),就是根据另一个AnthorMap.config文件来生成SqlMapper。

AnthorMap.config和默认的SqlMap.config一样,只是根据你的数据不同设置不同而已,测试AnthorMap.config如下如下:
<? 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" />    
    
</ settings >    
   
  
< providers  resource ="ServerConfig/providers.config" />    
   
  
<!--  Database connection information  -->    
  
< database >    
    
< provider  name ="sqlServer2.0" />    
    
< dataSource  name ="CrmSystem"  connectionString ="server=.;database=TestDB;uid=sa;pwd=" />    
  
</ database >    
   
    
< sqlMaps >    
    
< sqlMap  embedded ="Test.Domain.Weather.xml,Test.Domain"   />    
        
   
  
</ sqlMaps >    
        
</ sqlMapConfig >    
接下来就可以使用AntherMapper来创建ISqlMapper了。如下:
public  IList < Weather >  GetWeather()    
{    
     ISqlMapper map 
= AnthorMapper.Instance();    
   
     
return map.QueryForList<Weather>("Weather.Select"null);    
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值