`
maplye
  • 浏览: 111783 次
  • 来自: ...
社区版块
存档分类
最新评论

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);    
}
 

分享到:
评论

相关推荐

    iBATIS实战

    1.4.2 被多个分散的系统访问 21 1.4.3 复杂的键和关系 21 1.4.4 数据模型的去规范化或过度规范化 22 1.4.5 瘦数据模型 23 1.5 小结 24 第2章 iBATIS是什么 26 2.1 映射SQL语句 27 2.2 iBATIS如何工作 29 2.2.1 ...

    流风通用管理框架(源码+数据库)

    用户管理: 管理系统用户信息,并可以定义用户所属的角色,部门,岗位,一个用户可以选择多个角色 部门管理: 管理系统部门信息 岗位管理: 管理系统岗位 在线用户: 查看当前登录系统的用户 参考界面: 代码生成器: ...

    流风通用管理框架源码

    query: Ajax技术的应用 ...用户管理: 管理系统用户信息,并可以定义用户所属的角色,部门,岗位,一个用户可以选择多个角色 部门管理: 管理系统部门信息 岗位管理: 管理系统岗位 在线用户: 查看当前登录系统的用户

    IBatisNet完整项目源码(含数据库)

    Include :iBatis.Net架构的dll文件 Log :放置log文件的文件夹,在web.config里配置 1.在一些特定的环境下,一站式的解决方案未必有效 系统的部分或全部数据来自现有数据库,处于安全考虑,只对开发团队提供几条...

    IBatisNet.DataMapper 之简单三层

    Include :iBatis.Net架构的dll文件 Log :放置log文件的文件夹,在web.config里配置 1.在一些特定的环境下,一站式的解决方案未必有效 系统的部分或全部数据来自现有数据库,处于安全考虑,只对开发团队提供几条...

    通用管理框架正式版1.1源码

    用户管理: 管理系统用户信息,并可以定义用户所属的角色,部门,岗位,一个用户可以选择多个角色 部门管理: 管理系统部门信息 岗位管理: 管理系统岗位 在线用户: 查看当前登录系统的用户 参考界面: 代码生成器: ...

    通用管理框架源码下载 

    IBatis.Net是从Ibatis的Java版本移植过来的.NET版本。iBATIS作 为一种独特的Data Mapper,使用SQL映射的方式将对象持久化至关系型数据库。简单的理解就是它将我们在数据访问层实现的C#逻辑代码,变为通过 关系...

    [Java]Ibatis Spring Integration Demo

    动手写了一个Ibatis的Demo,反正啥东西,看着都是挺停简单的,Ibatis不就是一个ORM访问数据的东西嘛,不过在实际的动手的过程之中,还是需要到一些问题: 1)缺少这包,缺少那包的;还好用maven只要知道包的版本和2...

    AppFramework数据库访问组件_代码生成插件_V1.1.rar

    除了实现数据的增删改查,数据访问层还要提供一些与业务无关功能,例如面向对象的持久化与访问机制、本地事务与分布式事务支持、多数据库支持,这些机制或功能形成相对独立的逻辑领域,其主要目的有: &lt;br&gt;1、 ...

    AppFramework_V1.0

    除了实现数据的增删改查,数据访问层还要提供一些与业务无关功能,例如面向对象的持久化与访问机制、本地事务与分布式事务支持、多数据库支持,这些机制或功能形成相对独立的逻辑领域,其主要目的有: &lt;br&gt;1、 ...

    AppFramework_V1.0_New

    除了实现数据的增删改查,数据访问层还要提供一些与业务无关功能,例如面向对象的持久化与访问机制、本地事务与分布式事务支持、多数据库支持,这些机制或功能形成相对独立的逻辑领域,其主要目的有: &lt;br&gt;1、 ...

    java文集

    DFS文件读写 网络爬虫之Spider Java正则表达式的总结关键词: Java正则表达式 批量上传--采集 (多个文件夹) The Agile Way hibernate mapping文件中的标记详解:关系标记 ANT 安装使用及...

    iuhyiuhkjh908u0980

    一个组件可以有一个或多个支持它的数据库,因此,当装配两个或更多的组件时,我们希望能够保持在跨组件的多个数据库中进行的操作的原子性。J2EE服务器为这些组件提供了一个容器来保证事务原子性和跨组件独立性。如果...

    jfinalpluginsjfinal-dreampie.zip

    https://github.com/Dreampie?tab=repositories 分割成多个独立的插件进行优化,可以看我的博客拆分优化情况 http://my.oschina.net/wangrenhui1990/blog demo:(Angularjs前端 jfinal-dreampie插件) ...

Global site tag (gtag.js) - Google Analytics