IBatis.Net study notes three - two common DAO

In ibatis we can dao type of flexible options, that is, you can choose a different mode of operation in the underlying database. There are conventional ways, the profile of the way, hibernet ways such as:
1, the conventional way
and we develop more similar to previous ado.net, sql statement will write all the code in cs call:
first initialize through a configuration file:

            domdaomanagerbuilder builder  =  new  domdaomanagerbuilder();
            builder.configure(
" dao "  +  " _ "  +  configurationmanager.appsettings[ " database " +  " _ "
                
+  configurationmanager.appsettings[ " providertype " +  " .config " );
            daomanager 
=  daomanager.getinstance( " simpledao " );        

Corresponding to the configuration file as follows:

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

Then in the corresponding, for example accountdao write specific query sql etc.

2 configuration
will sql statement in a configuration file, write and modify more flexible, which is more commonly used way
is first initialized by the configuration file:

            domdaomanagerbuilder builder  =  new  domdaomanagerbuilder();
            builder.configure(
" dao "  +  " _ "  +  configurationmanager.appsettings[ " database " +  " _ "
                
+  configurationmanager.appsettings[ " providertype " +  " .config " );
            daomanager 
=  daomanager.getinstance( " sqlmapdao " );        

Corresponding to the configuration file as follows:

     < 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 statement of each may then be placed in a separate table a profile, such as:

         < 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

Guess you like

Origin blog.csdn.net/weixin_34352449/article/details/93251818