iBATIS.NET多数据库支持浅析
创始人
2024-04-18 19:51:11
0

谈到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语句的写法浅析

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...