IBatis.Net学习笔记五--常用的查询方式

在项目开发过程中,查询占了很大的一个比重,一个框架的好坏也很多程度上取决于查询的灵活性和效率。
在ibatis.net中提供了方便的数据库查询方式。

在dao代码部分主要有两种方式:
1、查询结果为一个对象:

                isqlmapper sqlmap  =  sqlmapdaosession.sqlmap;

                
return  (account) sqlmap.queryforobject( " getaccountviacolumnname " , accountid);

2、查询结果为一个列表:

                isqlmapper sqlmap  =  sqlmapdaosession.sqlmap;

                
return  (arraylist)sqlmap.queryforlist( " getaccountashashtableresultclass " 1 );

这两种方法同时都提供了面向泛型的重载方法。这两个方法的第一个参数对应配置文件中的select id,第二个参数表示传入查询的条件

配置文件的写法:
在ibatis.net中提供了多种查询配置的写法,我这里列出几种比较常用的方式:
1、获得一张表的所有数据

         < select id = " getallaccountsashashmapviaresultmap "
                        resultmap
= " account-hashtable-result " >
            select 
*
            from accounts
            order by account_id
        
</ select >

这是最简单的方式,其中resultmap是返回查询结果的形式,需要另外配置:

         < resultmap id = " account-hashtable-result "  class = " hashtable " >
            
< result property = " id "            column = " account_id " />
            
< result property = " firstname "     column = " account_firstname " />
            
< result property = " lastname "      column = " account_lastname " />
            
< result property = " emailaddress "  column = " account_email " />
        
</ resultmap >

表示:得到的结果的每一条记录都映射成一个hashtable,这个hashtable中包含四个key(id,firstname......)

2、根据条件查询(简单方式):

         < select id = " getaccountviacolumnindex "
                parameterclass
= " int "
                resultmap
= " indexed-account-result " >
            select
            account_id,
            account_firstname,
            account_lastname,
            account_email
            from accounts
            where account_id 
=  #value#
        
</ select >

只有一个条件,传入参数的类型是int型,拼写sql时直接用 #value#就可以了

3、根据条件查询(较复杂方式):

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

传入参数是一个hashtable,maximumallowed等表示的是hashtable里的key值,用$$包含起来。
并且查询时可以根据条件是否为空动态拼写sql语句
ps:输入参数同样可以使用account类,注意对应的键要和类中的属性名一致(大小写也要一样)

4、多表查询
多表查询时返回参数有三种方式,一种是新建一个类,在这个类中包含这多个表的所有属性,还有一种就是直接返回hastable就可以了:

         < select id = " getaccountashashtableresultclass "
    resultclass
= " hashmap " >
            select
            a.
* ,b. *
            from a,b
            where a.account_id 
=  b.account_id         </ select >

ps:这里的hashmap实际上就是hashtable

第三种方式是使用ibatis中的复杂属性(感谢anders cui 的提醒)
比如现在有两张表account和degree,使用account_id关联,那么需要在原有的基础上修改:
1、修改account实体类,加入一个属性:

         private  degree _degree;
        
public  degree degree
        
{
            
get
            
{
                
return _degree;
            }

            
set
            
{
                _degree 
= value;
            }

        }

这样是一个1:1的关系,也可以加入ilist degreelist的属性,这样查询的结果就是一个1:n的关系

2、修改配置文件:
在resultmaps节加入:

     < resultmap id = " comresult "    class = " account "  >
      
< result property = " id "            column = " account_id " />
      
< result property = " firstname "     column = " account_firstname " />
      
< result property = " lastname "      column = " account_lastname " />
      
< result property = " emailaddress "  column = " account_email "  nullvalue = " no_email@provided.com " />
      
< result property = " degree "  column = " account_id=account_id "   select = " degreeretrive "  />
    
</ resultmap >

对于degree属性,还可以加入lazyload=true 延迟加载,优化性能(也就是开始时并没有实际查询数据库,当用到属性degree时,才实际的查询相应的数据)

在statements节加入:

     < statement id = " degreeretrive "
      parameterclass
= " hashtable "
      resultclass
= " degree " >
      select 
*
      from degree
      where account_id 
=  #account_id#
    
</ statement >

    
< select id = " getcomtables "
      resultmap
= " comresult " >
      select 
*
      from accounts
      order by account_id
    
</ select >

这样可以正确的查询出结果,符合oo,但是也有两个小问题:
1、比较麻烦,不够灵活
2、性能受影响:
    这种方式其实和hibernet比较类似了,查询时首先执行
    select *        from accounts        order by account_id
    然后根据这条语句的结果,比如有100条记录,那就要执行100次以下的语句:
    select *        from degree        where account_id =  @param0

关于输入输出:
从上面可以看到输入时可以使用:parameterclass和parametermap,输出时可以使用:resultclass和resultmap
对于resultmap和parametermap我们需要另外进行配置(如上所示)
对于parameterclass和resultclass,如果是c#固有类型可以直接使用,如果是我们自定义类可以在sqlmap.config中先统一声明一下:

     < alias >
        
< typealias alias = " account "  type = " gspring.domain.account " />
    
</ alias >

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值