IBatis.Net study notes five - common query

In the project development process, a query accounted for a large proportion, a good or bad frame of a lot of flexibility and efficiency depends in part on the query.
It provides a convenient way ibatis.net the database query.

In the code portion dao mainly in two ways:
1, the query result is an object:

                isqlmapper sqlmap  =  sqlmapdaosession.sqlmap;

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

2, the query results as a list:

                isqlmapper sqlmap  =  sqlmapdaosession.sqlmap;

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

Both methods are provided for generics overloaded method. The first parameter of these two methods select id corresponding to the configuration file, the second parameter indicates the condition of the incoming queries

profiles wording:
provides a variety of writing queries configured in ibatis.net, list I have here several commonly used methods:
1, to obtain a list of all the data

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

This is the easiest way, which is resultmap return query results in the form, require additional configuration:

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

Represents: Each record results obtained are mapped into a hashtable, that the hashtable comprises four Key (ID, FirstName ......)

2, according to the query condition (simple method):

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

Only one condition, the type of arguments passed is int type, spelling sql directly on it # value #

3, according to the conditions of inquiry (more complex way):

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

Parameters are passed in a hashtable, maximumallowed etc. represents the hashtable where the key value, with $$ contain them.
And depending on whether the condition is empty spelling dynamic sql statement query
ps: Input parameters can also use the account class, pay attention to the key corresponding to the same attribute names and class (as the case should be)

4, multi-table queries
multi-table query there are three ways return parameters, one is a new category, which contains all the attributes of the plurality of tables in this class, there is a direct return hastable can be:

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

ps: hashmap hashtable here is actually

a third way is to use complex property ibatis in ( thanks anders cui reminder )
example, there are two tables account and degree, using account_id association, you need to modify the basis of the original:
1, modified account entity class, a property is added:

         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 = " [email protected] " />
      
< 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 >

Original Address: http://www.cnblogs.com/firstyi/archive/2007/08/21/863605.html

Reproduced in: https: //www.cnblogs.com/zyfking/archive/2009/01/19/1378511.html

Guess you like

Origin blog.csdn.net/weixin_34303897/article/details/93251756