iBATIS.NET多数据库支持浅析

开发 后端
iBATIS.NET多数据库支持浅析向你介绍iBATIS.NET的特性,iBATIS.NET多数据库支持是编程人员的福音。

谈到iBATIS.NET多数据库支持我们首先来看看它本身的帮助文档,在iBATIS.NET的帮助文档中有介绍多数据库支持,但是没有写全代码,后来查看其源码,并结合帮助文档,找到了解决方法,其实道理就是另行实现一个Mapper.

iBATIS.NET多数据库支持实例如AnthorMapper:

  1. Apache Notice#region Apache Notice      
  2.  
  3. #endregion      
  4.      
  5. using IBatisNet.Common.Utilities;      
  6. using IBatisNet.DataMapper;      
  7. using IBatisNet.DataMapper.Configuration;      
  8.      
  9. namespace IBatisNet.DataMapper      
  10. {      
  11.     /**//// ﹤summary﹥     
  12.     /// A singleton class to access the default SqlMapper defined by the SqlMap.Config      
  13.     /// ﹤/summary﹥     
  14.     public sealed class AnthorMapper      
  15.     {     
  16.         Fields#region Fields      
  17.         private static volatile ISqlMapper _mapper = null;     
  18.         #endregion      
  19.      
  20.         /**//// ﹤summary﹥     
  21.         ///       
  22.         /// ﹤/summary﹥     
  23.         /// ﹤param name="obj">﹤/param﹥     
  24.         public static void Configure (object obj)      
  25.         {      
  26.             _mapper = null;      
  27.         }      
  28.      
  29.         /**//// ﹤summary﹥     
  30.         /// Init the 'default' SqlMapper defined by the SqlMap.Config file.      
  31.         /// ﹤/summary﹥     
  32.         public static void InitMapper()      
  33.         {      
  34.             ConfigureHandler handler = new ConfigureHandler (Configure);      
  35.             DomSqlMapBuilder builder = new DomSqlMapBuilder();      
  36.             _mapper = builder.ConfigureAndWatch ("AnthorMap.config",handler);      }      
  37.      
  38.         /**//// ﹤summary﹥     
  39.         /// Get the instance of the SqlMapper defined by the SqlMap.Config file.      
  40.         /// ﹤/summary﹥     
  41.         /// ﹤returns>A SqlMapper initalized via the SqlMap.Config file.﹤/returns﹥     
  42.         public static ISqlMapper Instance()      
  43.         {      
  44.             if (_mapper == null)      
  45.             {      
  46.                 lock (typeof (SqlMapper))      
  47.                 {      
  48.                     if (_mapper == null// double-check      
  49.                     {         
  50.                         InitMapper();      
  51.                     }      
  52.                 }      
  53.             }      
  54.             return _mapper;      
  55.         }      
  56.               
  57.         /**//// ﹤summary﹥     
  58.         /// Get the instance of the SqlMapper defined by the SqlMap.Config file. (Convenience form of Instance method.)      
  59.         /// ﹤/summary﹥     
  60.         /// ﹤returns>A SqlMapper initalized via the SqlMap.Config file.﹤/returns﹥     
  61.         public static ISqlMapper Get()      
  62.         {      
  63.             return Instance();      
  64.         }  
  65.     }  
  66. }  

以上代码只是修改了iBATIS.NET中的Mapper的代码,将_mapper = builder.ConfigureAndWatch (handler);修改为_mapper = builder.ConfigureAndWatch ("AnthorMap.config",handler),就是根据另一个AnthorMap.config文件来生成SqlMapper。

AnthorMap.config和默认的SqlMap.config一样,只是根据你的数据不同设置不同而已,测试AnthorMap.config如下如下:

  1. ﹤?xml version="1.0" encoding="utf-8"?﹥    
  2. ﹤sqlMapConfig       
  3.   xmlns="http://ibatis.apache.org/dataMapper"       
  4.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"﹥    
  5.      
  6.   ﹤settings﹥    
  7.         ﹤setting useStatementNamespaces="true"/﹥    
  8.     ﹤/settings﹥    
  9.      
  10.   ﹤providers resource="ServerConfig/providers.config"/﹥    
  11.      
  12.   ﹤!-- Database connection information --﹥    
  13.   ﹤database﹥    
  14.     ﹤provider name="sqlServer2.0"/﹥    
  15.     ﹤dataSource name="CrmSystem" connectionString="server=.;database=TestDB;uid=sa;pwd="/﹥    
  16.   ﹤/database﹥    
  17.      
  18.     ﹤sqlMaps﹥    
  19.     ﹤sqlMap embedded="Test.Domain.Weather.xml,Test.Domain" /﹥    
  20.           
  21.      
  22.   ﹤/sqlMaps﹥    
  23.           
  24. ﹤/sqlMapConfig﹥ 

iBATIS.NET多数据库支持之使用AntherMapper来创建ISqlMapper了。如下:

  1. public IList﹤Weather﹥GetWeather()      
  2. {      
  3.      ISqlMapper map = AnthorMapper.Instance();      
  4.      
  5.      return map.QueryForList﹤Weather>("Weather.Select"null);      
  6. }  

那么iBATIS.NET多数据库支持就介绍到这里,希望这样的介绍对你有帮助。

【编辑推荐】

  1. 动态Mapped Statement在iBATIS中应用
  2. iBATIS中添加DAO的配置浅析
  3. iBATIS DAO framework初体验
  4. iBATIS教程之快速入门浅析
  5. iBATIS教程之like语句的写法浅析
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-07-20 09:51:19

iBATIS.net数据库缓存

2009-07-20 15:14:44

iBATIS.NET连

2009-07-22 09:07:01

iBATIS.NET

2009-07-20 13:22:47

iBATIS.Net日

2009-07-20 14:56:18

iBATIS.NET动态选择DAO

2009-07-20 10:06:07

iBATIS.net查询方式

2009-07-21 13:50:00

iBATIS.NET调

2009-07-20 13:47:08

iBATIS.NET字

2009-07-21 16:30:15

iBATIS.NET与单元测试

2009-07-16 13:50:31

ibatisResultMap

2009-07-21 17:06:35

iBATIS.NET执

2009-07-22 14:28:52

iBATIS.NET配

2009-07-20 09:27:42

IBATIS.netDAO

2009-07-22 14:11:09

配置ibatis.neiBatis.net配

2009-07-21 14:15:00

iBATIS.NET多

2009-07-20 15:27:22

Castle.DynaiBATIS.NET

2011-03-15 13:30:27

IBatis.netMySQL

2009-07-17 17:57:20

NPetShop iBATIS.Net

2009-07-31 09:57:47

ASP.NET数据库缓

2009-07-28 17:36:21

ASP.NET数据库连
点赞
收藏

51CTO技术栈公众号