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 " );        

相对应的配置文件如下:

     < 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 >

然后在对应的,比如accountdao中写具体的查询sql等

2、配置方式
将sql语句放在配置文件中,书写和修改较灵活,这也是比较常用的方式
首先通过配置文件初始化:

            domdaomanagerbuilder builder  =  new  domdaomanagerbuilder();
            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 >

然后可以将每一张表的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 >

3、使用hibernet方式
也就是使用hibernet的数据库操作。

原文地址: http://www.cnblogs.com/firstyi/archive/2007/08/17/859772.html

转载于:https://www.cnblogs.com/zyfking/archive/2009/01/19/1378506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值