iBatisNet学习笔记一:配置

从网上下载了一个IBatisNet的Demo,并结合相关文章进行理解学习。

Demo的相关配置文件包括:sqlmap.config, database.config, providers.config, web.config, 以及Person.xml文件。

下面进行学习:

参考文章:

http://www.cnblogs.com/pw/archive/2006/08/08/470060.html


首先了解sqlmap.config

IBatisNet DataMapper是通过XML文件来配置的,配置文件名称我们通常默认为SqlMap.Config,配置文件中指定了我们项目的数据库连接字符串,以及数据库表的映射文件等等.

sqlmap.config文件:

 

sqlmap.config
<?xml version="1.0" encoding="utf-8"?>  
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >  
  
  
<!-- Rem : If used via a DataAccess context, properties tag will be ignored    
  <properties resource="http://www.cnblogs.com/database.config"/> 
-->  
  
<properties embedded="database.config, IBatisNetDemo"/>  
  
<settings>  
    
<setting useStatementNamespaces="${useStatementNamespaces}"/>  
    
<setting cacheModelsEnabled="true"/>  
    
<setting validateSqlMap="false"/>  
  
</settings>  
    
  
<!-- Optional if resource -->  
  
<providers embedded="providers.config,IBatisNetDemo"/>  
  
<!-- ==== SqlClient configuration =========    -->  
  
<!-- Rem : If used via a DataAccess context, database tag will be ignored -->  
  
<database>  
    
<!-- Optional ( default ) -->  
    
<provider name="sqlServer2.0"/>  
    
<dataSource name="iBatisNet" connectionString="data source=${datasource};database=${database};user id=${userid};password=${password};connection reset=false;connection lifetime=5; min pool size=1; max pool size=50"/>  
  
</database>  
  
  
<sqlMaps>  
    
<!-- user via embedded-->  
    
<sqlMap embedded="Map.SqlClient.Person.xml,IBatisNetDemo"/>  
  
</sqlMaps>  
</sqlMapConfig>  

 

 

解释:

1.properites

<properties embedded="database.config, IBatisNetDemo"/>  指定properties文件可作为程序集的资源文件进行加载


2.setting

useStatementNamespaces 是否使用Satement命名空间,这里的命名空间指的是映射文件中sqlMap节点的namespace属性

映射文件Person.xml:<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
cacheModelsEnabled 是否启用DataMapper的缓存机制,针对全部的SqlMap(什么是DataMap的缓存机制?)

validateSqlMap 是否启用SqlMapConfig.xsd schema验证映射文件


3.providers
用于提供数据库驱动配置文件的文件名和路径

<providers embedded="providers.config,IBatisNetDemo"/>


4.database

其中包括两个子元素,分别是provider和datasource
如果在providers.config文件中指定了默认的数据库驱动,那么provider节点就不需要设置了,它的作用是在换数据库驱动时不需要修改providers.config文件。datasource节点用于指定ADO.NET Connection String.
<database>
    <!-- Optional ( default ) -->
    <provider name="sqlServer2.0"/>
    <dataSource name="iBatisNet" connectionString="data source=${datasource};database=${database};user id=${userid};password=${password};connection reset=false;connection lifetime=5; min pool size=1; max pool size=50"/>
</database>


5.sqlmap

该节点下需列出所有应用程序使用的DataMapper的实例,也就是映射文件。
<sqlMap embedded="Map.SqlClient.Person.xml,IBatisNetDemo"/>


其中,带$号的变量,在database.config中配置,并通过<properties embedded="database.config, IBatisNetDemo"/>加载。

database.config文件:

database.config
<?xml version="1.0" encoding="utf-8" ?>  
<databaseConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >  
<settings>  
    
<add key="userid" value="sa" />  
    
<add key="password" value="1234" />  
    
<add key="database" value="iBatis" />  
    
<add key="datasource" value="(local)" />  
    
<add key="selectKey" value="select @@IDENTITY as value" />  
    
<add key="directory" value="Maps" />  
    
<add key="useStatementNamespaces" value="false" />  
</settings>  
</databaseConfig>  

 

 也可以直接把数据库参数写在sqlmap.config文件中,而不需要database.config文件。

posted @ 2009-12-24 10:11  牛小花  阅读(855)  评论(3编辑  收藏  举报