Configuring the DataMapper for .NET

iBatis DataMapper配置文件 SqlMap.config ,在这里配置数据库,数据映射,其他如缓存,事务,多线程等。在运行时,iBatis一个类会加载,解析SqlMap.config文件,解析之后会返回DataMapper Client(an instance of SqlMapper)。

每个配置文件(SqlMap.config)可以指定一个数据库连接。但是在程序中可以使用多个DataMapper,只需要创建并使用另一个配置文件(SqlMap.config).配置文件可以使用相同的数据库不同的用户名,也可以使用在不同机器上的数据库。可以一个DataMapper读一个写。

4.3.2. DataMapper Configuration File (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" >

  <!-- Optional -->
  <properties resource="properties.config"/>

  <settings>
    <setting useStatementNamespaces="false"/>
    <setting cacheModelsEnabled="true"/>
    <setting validateSqlMap="false"/>
  </settings>

  <!-- Not required if providers.config is located in default location -->
  <providers embedded="resources.providers.config, IBatisNet.Test"/>

  <database>
    <provider name="sqlServer1.1"/>
    <dataSource name="NPetshop" 
                connectionString="user id=${username};
                password=${password};
                data source=${datasource};
                database=${database};"/>
  </database>

  <alias>
    <typeAlias alias="Account" type="IBatisNet.Test.Domain.Account, IBatisNet.Test"/>
    <typeAlias alias="YesNoBool"
         type="IBatisNet.Test.Domain.YesNoBoolTypeHandlerCallback, IBatisNet.Test"/>
  </alias>

  <typeHandlers>
    <typeHandler type="bool" dbType="Varchar" callback="YesNoBool"/>
  </typeHandlers>

  <sqlMaps>
    <sqlMap resource="${root}Maps/Account.xml"/>
    <sqlMap resource="${root}Maps/Category.xml"/>
    <sqlMap resource="${root}Maps/Product.xml"/>
  </sqlMaps> 
</sqlMapConfig>
View Code

4.3.3. DataMapper Configuration Elements

4.3.3.1. The <properties> Element

<?xml version="1.0" encoding="utf-8" ?> 
<settings>
  <add key="username" value="albert" />
</settings>

<dataSource connectionString="user id=${username};" 
View Code

使用propertyies file

<properties resource="propertyies.config">
<property key="host" value="ibatis.com" />
</properties>

<properties>
 <property resource="propertyies.config"/>
 <property resource="anotherProperties.config"/>
 <property key="host" value="ibatis.com" />
</properties>
View Code

引入文件的方式

    • resource : resource="properties.config"
    • url : url="c:\Web\MyApp\Resources\properties.config"   -or-   url="file://c:\Web\MyApp\Resources\properties.config"
    • embedded :embedded="Resources.properties.config, MyApp.Data"

4.3.3.2. The <providers> Element

<providers resource="Providers.config"/>

provider.config 文件 使用哪种provider 需要 设置其 enabled=“true”

4.3.3.3. The <settings> Element

<settings>
<setting useStatementNamespaces="true"/> map statement是否使用命名空间(默认关闭)
<setting cacheModelsEnabled="true"/> 是否使用缓存(默认开启)
</settings>

4.3.3.4. The <typeAlias> Element

4.3.3.4.2. Predefined type aliases预定义别名

4.3.3.5. The <typeHandler> Element

<typeHandler type="guid" dbType="Varchar2" callback="GuidVarchar"/>

4.3.3.6. The <database> Element

<!-- The ${properties} are defined in an external file, -->
<!-- but the values could also be coded inline. -->

<!-- Connecting to SQL Server -->
<database>
  <provider name="sqlServer1.1" />
  <dataSource name="NPetstore" default="true" 
       connectionString="data source=(local)\NetSDK;database=${database};
       user id=${username};password=${password};"/>
</database>

<!-- Connecting to Oracle -->
<database>
  <provider name="oracleClient1.0"/>
  <dataSource name="iBatisNet" 
       connectionString="Data Source=${datasource};User Id=${userid};Password=${password}"/>
</database>
 
<!-- Connecting to Access -->
<database>
  <provider name="OleDb1.1" />
  <dataSource name="NPetstore" default="true" 
       connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=${database}"/>
</database>

<!-- Connecting to a MySQL database --> 
<database>
  <provider name="ByteFx" />
  <dataSource name="NPetstore" default="true" 
       connectionString="Host=${host};Database=${database};
       Password=${password};Username=${username}" />
</database>

<!-- Connecting to a PostgreSQL database --> 
<database>
  <provider name="PostgreSql0.7" />
  <dataSource name="NPetstore" default="true" 
       connectionString="Server=${server};Port=5432;User Id=${userid};Password=${password};
       Database=${database};" />
</database>
View Code

4.3.3.7. The <sqlMap> Element

<!-- Relative path from the project root directory using a property variable -->
<sqlMaps>
  <sqlMap resource="${root}Maps/Account.xml"/>
  <sqlMap resource="${root}Maps/Category.xml"/>
  <sqlMap resource="${root}Maps/Product.xml"/>
</sqlMaps>

<!-- Embedded resources using [extendednamespace.]filename, assemblyname -->
<sqlMaps>
  <sqlMap embedded="Maps.Account.xml, MyApp.Data"/>
  <sqlMap embedded="Maps.Category.xml, MyApp.Data"/>
  <sqlMap embedded="Maps.Product.xml, MyApp.Data"/>
</sqlMaps>

<!-- Full URL with a property variable -->
<sqlMaps>
  <sqlMap url="C:/${projectdir}/MyApp/Maps/Account.xml"/>
  <sqlMap url="C:/${projectdir}/MyApp/Maps/Category.xml"/>
  <sqlMap url="C:/${projectdir}/MyApp/Maps/Product.xml"/>
</sqlMaps>
View Code

 

posted @ 2020-03-28 17:03  vvf  阅读(92)  评论(0编辑  收藏  举报