代码改变世界

实例化SqlMapper的几种方式_IBatis.net

2011-09-22 16:54  elivsit  阅读(624)  评论(0编辑  收藏  举报

IBatis.net作为一个Data Mapper 框架,SqlMapper类就是应用程序跟它打交道的核心。如何从配置文件中实例化SqlMapper类,有以下几种方式。并加以区别,请根据实际情况选择合适的方式进行编码。

 

方式一:代码如下,DomSqlMapBuilder对象在默认项目类型目录下(如果不指定目录的话)查找SqlMap.config文件(如果不指定config文件的话), DomSqlMapBuilder.ConfigureAndWatch() 方法监视配置文件的变动. 一旦配置文件(config和Sql map文件)更改,  SqlMapper 实例将重新安全加载.  这在开发中尤其有用, 当你在启动调试的测试中,改变data map 文件可以立即看到结果,而不需求从新启动调试。同样,在生产环境中,可以更改这些配置,而不需要重新加载应用程序的其他部分。

 配置文件路径:

1,使用相对路径,即相对于应用程序根目录。 

2,放在应用程序根根目录。 根据项目类型不同而不同,就是Web.config或app.config所目录。如果是建立单独的测试项目,就要放在编译目录,为什么是编译目录,因为DomSqlMapBuilder对象是在编译目录下查找配置文件,如果你不知道编译目录在哪里。那就把配置文件放置在根目录下,或者根目录下任何一个文件夹里,并把配置文件的属性设置为“始终复制”或“如果较新则复制”。编译时会自动把配置文件复制到编译根目录。

3,使用绝对路径,该路径可以配在应用程序的配置文件中,通过代码读取,部署方便。

注意:使用 DomSqlMapBuilder.ConfigureAndWatch() ,必须设置应该程序有权限访问跟踪配置文件的更改。

 1         protected static void Configure(object obj)
 2         {
 3             _mapper = null;
 4         }
 5 
 6         protected static void InitMapper()
 7         {
 8             ConfigureHandler handler = new ConfigureHandler(Configure);
 9             DomSqlMapBuilder builder = new DomSqlMapBuilder();
10             _mapper = builder.ConfigureAndWatch("http://www.cnblogs.com/Config/SqlMap_Oracle_OracleClient.config", handler);
11         }

 DomSqlMapBuilder.ConfigureAndWatch()方法有三个重载,由于需要监视文件的更改,只能通过文件系统来获取SqlMap.config 文件. 所以只能用以下三个方法。这就是为什么只用三个重载的原因。

 1 /* Configure and monitor the configuration file for modifications 
 2    and automatically reconfigure the SqlMapper. 
 3    This basic ConfigureAndWatch method looks for a file with the 
 4    default name of SqlMap.config in the application root directory. */
 5 public ISqlMapper ConfigureAndWatch(ConfigureHandler configureDelegate)
 6 
 7 /* Configure and monitor the configuration file for modifications 
 8    and automatically reconfigure the SqlMapper. 
 9    Uses a relative path from your application root 
10    or an absolute file path such as "file:\\c:\dir\a.config" */
11 public ISqlMapper ConfigureAndWatch( string resource, ConfigureHandler configureDelegate )
12 
13 /* Configure and monitor the configuration file for modifications 
14    and automatically reconfigure the SqlMapper. 
15    Uses a FileInfo instance for your config file. */
16 public ISqlMapper ConfigureAndWatch( FileInfo resource, ConfigureHandler configureDelegate )

 

 

 方式二:代码如如下,调用DomSqlMapBuilder.Configure() 方法,将在应该用程序根目录下查找文件名为SqlMap.config的文件.根目录随项目类型不同而不同,通常是放 web.config 或app.config 文件的目录.该方式不监视配置文件的变化,所以没有方式一的优势。配置文件的路径同方式一。 

 1         private static void InitMapper()
 2         {
 3             //ConfigureHandler hander = new ConfigureHandler(Configure);
 4             //DomSqlMapBuilder builder = new DomSqlMapBuilder();
 5             //_mapper = builder.ConfigureAndWatch("SybaseSqlMap.config", hander);
 6 
 7             //don't need ConfigureHandler class
 8             DomSqlMapBuilder builder = new DomSqlMapBuilder();
 9             _mapper = builder.Configure(ConfigurationSettings.AppSettings["rootPath"+ "SqlMap_Sybase_Odbc.config");
10         }

 DomSqlMapBuilder.Configure() 方法有六个重载:

 1 /*Configure s SqlMapper with the default SqlMap.config*/
 2 ISqlMapper mapper = builder.Configure();
 3 
 4 /*Configure s SqlMapper with the specified SqlMap.config*/
 5 mapper = builder.Configure(
 6          ConfigurationSettings.AppSettings["rootPath"]+"SqlMap.config");
 7 
 8 
 9 /* Configure a SqlMapper with FileInfo. */
10 FileInfo aFileInfo = someSupportClass.GetDynamicFileInfo();
11 ISqlMapper mapper = builder.Configure(aFileInfo);
12 
13 /* Configure a SqlMapper through a Uri. */
14 Uri aUri = someSupportClass.GetDynamicUri();
15 ISqlMapper anotherMapper = builder.Configure(aUri);
16 
17 /* Configure a SqlMapper with an XmlDocument */
18 XmlDocument anXmlDoc = someSupportClass.GetDynamicXmlDocument();
19 ISqlMapper mapper = builder.Configure(anXmlDoc);
20 
21 /* Configure a SqlMapper from a stream. */
22 Stream aStream = someSupportClass.GetDynamicStream();
23 ISqlMapper anotherMapper = builder.Configure(aStream);