IBatis.Net学习笔记三--两种常用的DAO
在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的数据库操作。
原文地址: http://www.cnblogs.com/firstyi/archive/2007/08/17/859772.html