iBatis.Net系列(四)-properties.config

來源:互聯網
上載者:User

標籤:

在上一節中我們說到使用核心檔案sqlmap.config的properties節點來引用另外的設定檔,這個設定檔就是properties.config,我們一般把 資料庫驅動、資料庫連接字串、sql語句對應檔的路徑 存放在這個檔案中,方便統一管理。
我們在上一節中
1 設定資料庫驅動是這樣寫的
<provider name="sqlServer4.0"/>
我們覺得吧sqlServer4.0這樣的驅動寫在核心檔案中不太科學,
2 資料庫連接字串是這樣配置的
<dataSource name="Home" connectionString="Data Source=127.0.0.1;Initial Catalog=Db2;User ID=sa;Password=123"/>
我們覺得把這麼長的資料庫連接字串寫在核心檔案中也不太科學
3 配置sql對應檔是這樣寫的
<sqlMaps>
<sqlMap resource="../IBNetModel/Maps/Student.xml"/>
<sqlMap resource="../IBNetModel/Maps/Course.xml"/>
</sqlMaps>
我們發現../IBNetModel/Maps/是重複的,如果有上百個這樣的檔案,那我們得重複寫上百次,不便於管理。
這一切都可以使用properties.config設定檔來解決,在該檔案中這樣配置:
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<add key="provider" value="sqlServer4.0" />
<add key="connectionString" value="Data Source=127.0.0.1;Initial Catalog=Db2;User ID=sa;Password=123"/>
<add key="root" value="../IBNetModel/Maps/" />
</settings>
這樣我們在sqlmap.config檔案中就可以通過${name}來引入配置了,其中name是指settings節點中的每一個key,比如${provider}就是指的sqlServer4.0,${connectionString}就是指的連接字串。

有了properties.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" >
<properties resource="properties.config"/>
<settings>
<setting useStatementNamespaces="false"/>
<setting cacheModelsEnabled="false"/>
<setting validateSqlMap="false"/>
</settings>
<database>
<provider name="${provider}"/>
<dataSource name="Home" connectionString="${connectionString}"/>
</database>
<sqlMaps>
<sqlMap resource="${root}Student.xml"/>
<sqlMap resource="${root}Course.xml"/>
</sqlMaps>
</sqlMapConfig>

 

iBatis.Net系列(四)-properties.config

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.