IBatis.Net常用的查询方式
来源:互联网 发布:ubuntu安装网页播放器 编辑:程序博客网 时间:2024/06/15 22:36
IBatis.Net常用的查询方式
在项目开发过程中,查询占了很大的一个比重,一个框架的好坏也很多程度上取决于查询的灵活性和效率。在IBatis.Net中提供了方便的数据库查询方式。
在Dao代码部分主要有两种方式:
1、查询结果为一个对象:
[运行代码] [复制到剪贴板] [ ± ]
CODE:ISqlMapper sqlMap = sqlMapDaoSession.SqlMap;
return (Account) sqlMap.QueryForObject("GetAccountViaColumnName", accountID);
2、查询结果为一个列表:
[运行代码] [复制到剪贴板] [ ± ]
CODE:ISqlMapper sqlMap = sqlMapDaoSession.SqlMap;
return (ArrayList)sqlMap.QueryForList("GetAccountAsHashtableResultClass", 1);
这两种方法同时都提供了面向泛型的重载方法。这两个方法的第一个参数对应配置文件中的select id,第二个参数表示传入查询的条件
配置文件的写法:
在IBatis.Net中提供了多种查询配置的写法,我这里列出几种比较常用的方式:
1、获得一张表的所有数据
[运行代码] [复制到剪贴板] [ ± ]
CODE: <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="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、根据条件查询(简单方式):
[运行代码] [复制到剪贴板] [ ± ]
CODE: <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>
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、根据条件查询(较复杂方式):
[运行代码] [复制到剪贴板] [ ± ]
CODE: <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>
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就可以了:
[运行代码] [复制到剪贴板] [ ± ]
CODE: <select id="GetAccountAsHashtableResultClass"
resultClass="HashMap">
select
a.*,b.*
from a,b
where a.Account_ID = b.Account_ID </select
resultClass="HashMap">
select
a.*,b.*
from a,b
where a.Account_ID = b.Account_ID </select
>S:这里的HashMap实际上就是Hashtable
第三种方式是使用IBatis中的复杂属性(感谢Anders Cui 的提醒)
比如现在有两张表Account和Degree,使用Account_ID关联,那么需要在原有的基础上修改:
1、修改Account实体类,加入一个属性:
[运行代码] [复制到剪贴板] [ ± ]
CODE: private Degree _degree;
public Degree Degree
{
get
{
return _degree;
}
set
{
_degree = value;
}
}
public Degree Degree
{
get
{
return _degree;
}
set
{
_degree = value;
}
}
这样是一个1:1的关系,也可以加入IList DegreeList的属性,这样查询的结果就是一个1:n的关系
2、修改配置文件:
在resultMaps节加入:
[运行代码] [复制到剪贴板] [ ± ]
CODE: <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@51aspx.com"/>
<result property="Degree" column="Account_ID=Account_ID" select="degreeretrive" />
</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" nullValue="no_email@51aspx.com"/>
<result property="Degree" column="Account_ID=Account_ID" select="degreeretrive" />
</resultMap>
对于Degree属性,还可以加入lazyLoad=true 延迟加载,优化性能(也就是开始时并没有实际查询数据库,当用到属性Degree时,才实际的查询相应的数据)
在statements节加入:
[运行代码] [复制到剪贴板] [ ± ]
CODE:<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中先统一声明一下:
[运行代码] [复制到剪贴板] [ ± ]
CODE: <alias>
<typeAlias alias="Account" type="GSpring.Domain.Account"/>
</alias>
<typeAlias alias="Account" type="GSpring.Domain.Account"/>
</alias>
- IBatis.Net常用的查询方式
- IBatis.Net常用的查询方式
- iBATIS.net常用的查询方式浅析
- IBatis.Net学习笔记五--常用的查询方式
- ibatis查询的两种映射方式
- IBatis.Net学习笔记三--两种常用的DAO
- iBatis的模糊查询
- ibatis的动态查询
- ibatis 的动态查询
- hibernate 三种常用的查询方式
- Hibernate 集中常用的查询方式
- ibatis.net的现状
- iBatis.Net的开发
- ibatis中动态查询中的常用属性
- EBS常用查询方式
- ASP.net中常用的缓存方式
- ASP.Net 中常用的数据库连接方式
- ibatis 的多条件查询
- 给自己报到。。。 。。。
- MATLAB命令大全
- Ubuntu 8.10安装后中文化及相关常用软件安装手记
- Ubuntu 文件编码转换
- 正则表达式30分钟入门教程
- IBatis.Net常用的查询方式
- emerge、eix、equery的基本用法[zz]
- 7.7
- 揭开正则表达式的神秘面纱
- 类的成员函数
- Subversion 备份版本库
- vc中字符串和数字转换的函数:atoi,atol,strtod,strtol,strtoul 类型转换
- 今天开始,好好学习
- 浅谈C中的malloc和free