在项目开发过程中,查询占了很大的一个比重,一个框架的好坏也很多程度上取决于查询的灵活性和效率。
在ibatis.net中提供了方便的数据库查询方式。
在dao代码部分主要有两种方式:
1、查询结果为一个对象:
return (account) sqlmap.queryforobject( " getaccountviacolumnname " , accountid);
2、查询结果为一个列表:
return (arraylist)sqlmap.queryforlist( " getaccountashashtableresultclass " , 1 );
这两种方法同时都提供了面向泛型的重载方法。这两个方法的第一个参数对应配置文件中的select id,第二个参数表示传入查询的条件
配置文件的写法:
在ibatis.net中提供了多种查询配置的写法,我这里列出几种比较常用的方式:
1、获得一张表的所有数据
resultmap = " account-hashtable-result " >
select *
from accounts
order by account_id
</ select >
这是最简单的方式,其中resultmap是返回查询结果的形式,需要另外配置:
< 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、根据条件查询(简单方式):
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 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就可以了:
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实体类,加入一个属性:
public degree degree
{
get
{
return _degree;
}
set
{
_degree = value;
}
}
这样是一个1:1的关系,也可以加入ilist degreelist的属性,这样查询的结果就是一个1:n的关系
2、修改配置文件:
在resultmaps节加入:
< 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节加入:
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中先统一声明一下:
< typealias alias = " account " type = " gspring.domain.account " />
</ alias >
原文地址: http://www.cnblogs.com/firstyi/archive/2007/08/21/863605.html