zoukankan      html  css  js  c++  java
  • IBatis.Net如何支持多个数据库

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

    在Ibatis.net的帮助文档中有介绍多数据库支持,但是没有写全代码,后来查看其源码,并结合帮助文档,找到了解决方法,其实道理就是另行实现一个Mapper.如AnthorMapper:
    Apache Notice    
       
    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    
       
            
    /// <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);    
    }
     

  • 相关阅读:
    QTP自动化测试项目管理最佳实践指南
    让自动化测试更“智能化”
    Selenium是否支持HTML5?
    QTP\UFT11.5破解
    亿能测试视频教程 QTP自动化测试视频系列(第26、27、28集)
    UIAutomator学习笔记V0.1
    TIB自动化测试工作室2012年资料汇总
    《Automated Software Testing》 2013年 4月 电子杂志下载
    QTP11.5全新自动化测试体验 移动终端测试
    亿能测试视频教程 QTP自动化测试视频系列(第10、11集)
  • 原文地址:https://www.cnblogs.com/maplye/p/934552.html
Copyright © 2011-2022 走看看