在IBatis中我们可以灵活的选择DAO类型,也就是可以在底层选用不同的数据库操作方式。有常规方式、配置文件的方式、Hibernet的方式等:
1、常规方式
和我们之前的ADO.NET开发较为类似,都是将sql语句写在cs代码中进行调用:
首先通过配置文件初始化:
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure("dao" + "_" + ConfigurationManager.AppSettings["database"] + "_"
+ ConfigurationManager.AppSettings["providerType"] + ".config");
daoManager = DaoManager.GetInstance("SimpleDao");
builder.Configure("dao" + "_" + ConfigurationManager.AppSettings["database"] + "_"
+ ConfigurationManager.AppSettings["providerType"] + ".config");
daoManager = DaoManager.GetInstance("SimpleDao");
相对应的配置文件如下:
<context id="SimpleDao" default="true">
<properties resource="http://www.cnblogs.com/database.config"/>
<!-- ==== SqlClient configuration (default provider) ========= -->
<database>
<!-- Optional ( default ) -->
<provider name="sqlServer1.1"/>
<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>
<daoFactory>
<dao interface="IBatisNet.DataAccess.Test.Dao.Interfaces.IAccountDao, IBatisNet.DataAccess.Test" implementation="IBatisNet.DataAccess.Test.Dao.Implementations.Ado.AccountDao, IBatisNet.DataAccess.Test"/>
</daoFactory>
</context>
<properties resource="http://www.cnblogs.com/database.config"/>
<!-- ==== SqlClient configuration (default provider) ========= -->
<database>
<!-- Optional ( default ) -->
<provider name="sqlServer1.1"/>
<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>
<daoFactory>
<dao interface="IBatisNet.DataAccess.Test.Dao.Interfaces.IAccountDao, IBatisNet.DataAccess.Test" implementation="IBatisNet.DataAccess.Test.Dao.Implementations.Ado.AccountDao, IBatisNet.DataAccess.Test"/>
</daoFactory>
</context>
然后在对应的,比如AccountDao中写具体的查询sql等
2、配置方式
将sql语句放在配置文件中,书写和修改较灵活,这也是比较常用的方式
首先通过配置文件初始化:
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure("dao" + "_" + ConfigurationManager.AppSettings["database"] + "_"
+ ConfigurationManager.AppSettings["providerType"] + ".config");
daoManager = DaoManager.GetInstance("SqlMapDao");
builder.Configure("dao" + "_" + ConfigurationManager.AppSettings["database"] + "_"
+ ConfigurationManager.AppSettings["providerType"] + ".config");
daoManager = DaoManager.GetInstance("SqlMapDao");
相对应的配置文件如下:
<context id="SqlMapDao">
<properties resource="http://www.cnblogs.com/database.config"/>
<!-- ==== SqlClient configuration ========= -->
<database>
<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>
<daoSessionHandler id="SqlMap">
<!-- -->
<property name="resource" value="SqlMap_MSSQL_SqlClient.config"/>
<!-- <property name="url" value="E:\Projet\iBatis\trunk\cs\mapper\IBatisNet.DataAccess.Test\bin\Debug\SqlMap_MSSQL_SqlClient.config"/>
-->
<!--
<property name="embedded" value="bin.Debug.SqlMap_MSSQL_SqlClient.config, IBatisNet.DataAccess.Test"/>
-->
</daoSessionHandler>
<daoFactory>
<dao interface="IBatisNet.DataAccess.Test.Dao.Interfaces.IAccountDao, IBatisNet.DataAccess.Test" implementation="IBatisNet.DataAccess.Test.Dao.Implementations.DataMapper.AccountDao, IBatisNet.DataAccess.Test"/>
</daoFactory>
</context>
<properties resource="http://www.cnblogs.com/database.config"/>
<!-- ==== SqlClient configuration ========= -->
<database>
<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>
<daoSessionHandler id="SqlMap">
<!-- -->
<property name="resource" value="SqlMap_MSSQL_SqlClient.config"/>
<!-- <property name="url" value="E:\Projet\iBatis\trunk\cs\mapper\IBatisNet.DataAccess.Test\bin\Debug\SqlMap_MSSQL_SqlClient.config"/>
-->
<!--
<property name="embedded" value="bin.Debug.SqlMap_MSSQL_SqlClient.config, IBatisNet.DataAccess.Test"/>
-->
</daoSessionHandler>
<daoFactory>
<dao interface="IBatisNet.DataAccess.Test.Dao.Interfaces.IAccountDao, IBatisNet.DataAccess.Test" implementation="IBatisNet.DataAccess.Test.Dao.Implementations.DataMapper.AccountDao, IBatisNet.DataAccess.Test"/>
</daoFactory>
</context>
然后可以将每一张表的sql语句单独放在一个配置文件中,比如:
<select id="GetAccountsDynamic" resultMap="account-result" parameterClass="Hashtable" >
select top $MaximumAllowed$ * from Accounts
<dynamic prepend="where">
<isParameterPresent>
<isNotEmpty prepend="and" property="FirstName" >
Account_FirstName LIKE '%$FirstName$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="LastName" >
Account_LastName LIKE '%$LastName$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="EmailAddress" >
Account_Email LIKE '%$EmailAddress$%'
</isNotEmpty>
</isParameterPresent>
</dynamic>
order by Account_LastName
</select>
select top $MaximumAllowed$ * from Accounts
<dynamic prepend="where">
<isParameterPresent>
<isNotEmpty prepend="and" property="FirstName" >
Account_FirstName LIKE '%$FirstName$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="LastName" >
Account_LastName LIKE '%$LastName$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="EmailAddress" >
Account_Email LIKE '%$EmailAddress$%'
</isNotEmpty>
</isParameterPresent>
</dynamic>
order by Account_LastName
</select>
3、使用Hibernet方式
也就是使用Hibernet的数据库操作。