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 = " ../../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 = " ../../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的数据库操作。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值