一个比较复杂的IBatis 动态查询条件事例
2011-04-08 18:52
351 查看
下面这个配置基本上包含了最复杂的功能:分页\搜索\排序\缓存\传值Hash表\返回hash表\动态sql
<statement id="XinxiTable_SelectAll" listClass="ArrayList" resultMap="SimpleXinxi" parameterClass="Hashtable" cacheModel="xinxi-cache" >
SELECT
<dynamic prepend="top">
<isNotEqual prepend="top" property="TopNum" compareValue = "0">
$TopNum$
</isNotEqual>
</dynamic>
*
FROM
(select a.[iXinxiID],a.[sXinxiTitle],a.[iXinxiClassId],b.[sClassName],
a.[dXinxiDate],a.[dXinxiYxq],a.[iXinxiHits],a.[sXinxiUser],a.[sRedirectUrl],
ROW_NUMBER() OVER(
<dynamic prepend="order by">
<isEqual prepend="order by" property="Sort" compareValue = "0">
a.iXinxiID desc
</isEqual>
<isEqual prepend="order by" property="Sort" compareValue = "1">
a.iXinxiID asc
</isEqual>
<isEqual prepend="order by" property="Sort" compareValue = "2">
a.iXinxiHits desc
</isEqual>
<isEqual prepend="order by" property="Sort" compareValue = "3">
a.iXinxiHits asc
</isEqual>
</dynamic>
) as row
FROM
[dbo].[XinxiTable] as a,[dbo].[XinxiClass] as b
<dynamic prepend="where">
<isParameterPresent>
<isNotEmpty prepend="and" property="XinxiType" >
a.[iXinxiState]= $XinxiType$
</isNotEmpty>
<isNotEqual prepend="and" property="XinxiClass" compareValue = "0">
a.[iXinxiClassID]= $XinxiClass$
</isNotEqual>
<isEqual prepend="and" property="SearchType" compareValue = "1">
a.[sXinxiTitle] LIKE '%$Keyword$%'
</isEqual>
<isEqual prepend="and" property="SearchType" compareValue = "2">
(a.[sXinxiTitle] LIKE '%$Keyword$%' or a.[sXinxiContent] LIKE '%$Keyword$%')
</isEqual>
</isParameterPresent>
</dynamic>
and a.iXinxiClassId=b.iClassId
)a
<dynamic prepend="where">
<isParameterPresent>
<isEqual prepend="and" property="IsPage" compareValue = "1">
row between $PageLower$ and $PageUpper$
</isEqual>
</isParameterPresent>
</dynamic>
</statement>
ibatis动态查询条件:
<select id="SelectEemployee" parameterClass="string" resultMap = "employee-result">
select * from employee
//动态SQL语句
<dynamic prepend="WHERE">
<isParameterPresent>
emp_id = #value#
</isParameterPresent>
</dynamic>
</select>
</statements>
</sqlMap>
/*动态SQL的写法:
开始 <dynamic
条件成立时前面要加的字符串 prepend ="字符串">
<属性关键字 (见下表)
prepend="字符串"
判断条件的对象属性名 property="字符串"
如果是属性关键字是比较条件时,字符串存放要比较的值compareValue="字符串">
要显示的条件名
</属性关键字>
结束</dynamic>
*/
/*动态SQL的参数有
属性关键字
含义
<isEqual>
如果参数相等于值则查询条件有效。
<isNotEqual>
如果参数不等于值则查询条件有效。
<isGreaterThan>
如果参数大于值则查询条件有效。
<isGreaterEqual>
如果参数等于值则查询条件有效。
<isLessEqual>
如果参数小于值则查询条件有效。如下所示:
<isLessEqual prepend = ”AND” property = ”age” compareValue = ”18” >
ADOLESCENT = ‘TRUE’
</isLessEqual>
<isPropertyAvailable>
如果参数有使用则查询条件有效。
<isNotPropertyAvailable>
如果参数没有使用则查询条件有效。
<isNull>
如果参数为NULL则查询条件有效。
<isNotNull>
如果参数不为NULL则查询条件有效。
<isEmpty>
如果参数为空则查询条件有效。
<isNotEmpty>
如果参数不为空则查询条件有效。参数的数据类型为Collection、String 时参数不为NULL或“”。如下所示:
<isNotEmpty prepend=”AND” property=”firstName” >
FIRST_NAME=#firstName#
</isNotEmpty>
<isParameterPresent>
如果参数类不为NULL则查询条件有效。
<isNotParameterPresent>
Checks to see if the parameter object is not present (null). Example Usage:
<isNotParameterPresent prepend=”AND”>
EMPLOYEE_TYPE = ‘DEFAULT’
</isNotParameterPresent>
<statement id="XinxiTable_SelectAll" listClass="ArrayList" resultMap="SimpleXinxi" parameterClass="Hashtable" cacheModel="xinxi-cache" >
SELECT
<dynamic prepend="top">
<isNotEqual prepend="top" property="TopNum" compareValue = "0">
$TopNum$
</isNotEqual>
</dynamic>
*
FROM
(select a.[iXinxiID],a.[sXinxiTitle],a.[iXinxiClassId],b.[sClassName],
a.[dXinxiDate],a.[dXinxiYxq],a.[iXinxiHits],a.[sXinxiUser],a.[sRedirectUrl],
ROW_NUMBER() OVER(
<dynamic prepend="order by">
<isEqual prepend="order by" property="Sort" compareValue = "0">
a.iXinxiID desc
</isEqual>
<isEqual prepend="order by" property="Sort" compareValue = "1">
a.iXinxiID asc
</isEqual>
<isEqual prepend="order by" property="Sort" compareValue = "2">
a.iXinxiHits desc
</isEqual>
<isEqual prepend="order by" property="Sort" compareValue = "3">
a.iXinxiHits asc
</isEqual>
</dynamic>
) as row
FROM
[dbo].[XinxiTable] as a,[dbo].[XinxiClass] as b
<dynamic prepend="where">
<isParameterPresent>
<isNotEmpty prepend="and" property="XinxiType" >
a.[iXinxiState]= $XinxiType$
</isNotEmpty>
<isNotEqual prepend="and" property="XinxiClass" compareValue = "0">
a.[iXinxiClassID]= $XinxiClass$
</isNotEqual>
<isEqual prepend="and" property="SearchType" compareValue = "1">
a.[sXinxiTitle] LIKE '%$Keyword$%'
</isEqual>
<isEqual prepend="and" property="SearchType" compareValue = "2">
(a.[sXinxiTitle] LIKE '%$Keyword$%' or a.[sXinxiContent] LIKE '%$Keyword$%')
</isEqual>
</isParameterPresent>
</dynamic>
and a.iXinxiClassId=b.iClassId
)a
<dynamic prepend="where">
<isParameterPresent>
<isEqual prepend="and" property="IsPage" compareValue = "1">
row between $PageLower$ and $PageUpper$
</isEqual>
</isParameterPresent>
</dynamic>
</statement>
ibatis动态查询条件:
<select id="SelectEemployee" parameterClass="string" resultMap = "employee-result">
select * from employee
//动态SQL语句
<dynamic prepend="WHERE">
<isParameterPresent>
emp_id = #value#
</isParameterPresent>
</dynamic>
</select>
</statements>
</sqlMap>
/*动态SQL的写法:
开始 <dynamic
条件成立时前面要加的字符串 prepend ="字符串">
<属性关键字 (见下表)
prepend="字符串"
判断条件的对象属性名 property="字符串"
如果是属性关键字是比较条件时,字符串存放要比较的值compareValue="字符串">
要显示的条件名
</属性关键字>
结束</dynamic>
*/
/*动态SQL的参数有
属性关键字
含义
<isEqual>
如果参数相等于值则查询条件有效。
<isNotEqual>
如果参数不等于值则查询条件有效。
<isGreaterThan>
如果参数大于值则查询条件有效。
<isGreaterEqual>
如果参数等于值则查询条件有效。
<isLessEqual>
如果参数小于值则查询条件有效。如下所示:
<isLessEqual prepend = ”AND” property = ”age” compareValue = ”18” >
ADOLESCENT = ‘TRUE’
</isLessEqual>
<isPropertyAvailable>
如果参数有使用则查询条件有效。
<isNotPropertyAvailable>
如果参数没有使用则查询条件有效。
<isNull>
如果参数为NULL则查询条件有效。
<isNotNull>
如果参数不为NULL则查询条件有效。
<isEmpty>
如果参数为空则查询条件有效。
<isNotEmpty>
如果参数不为空则查询条件有效。参数的数据类型为Collection、String 时参数不为NULL或“”。如下所示:
<isNotEmpty prepend=”AND” property=”firstName” >
FIRST_NAME=#firstName#
</isNotEmpty>
<isParameterPresent>
如果参数类不为NULL则查询条件有效。
<isNotParameterPresent>
Checks to see if the parameter object is not present (null). Example Usage:
<isNotParameterPresent prepend=”AND”>
EMPLOYEE_TYPE = ‘DEFAULT’
</isNotParameterPresent>
相关文章推荐
- 第一个文章,今天比较兴奋啊! 给大家一个关于SQL复合查询的文章(动态生成多个where条件)
- 【Sql语句】自己摸索出得比较复杂的一个查询语句
- 一个比较复杂的查询语句
- iBatis动态条件查询
- 【小小问题集锦6之---查询条件非常复杂时解决效率不高的一个办法】
- ibatis动态查询条件
- ibatis动态查询条件
- IBatisNet/IBatis动态条件查询XMl映射配置终极解决方案实例
- 如何进行ibatis动态多条件组合查询以及模糊查询(oracle,mysql)
- iBatis动态条件查询 where的if条件不执行
- 编写一个过程将查询的字段和where条件中的字符串值动态调用
- ibatis动态多条件查询及模糊查询(oracle,mysql,sql)
- jfinal的一个ConditionInterceptor拦截器,封装查询条件,拦截器中动态拼...
- iBatis动态条件查询
- ibatis动态查询条件
- ibatis动态查询条件(转载待完善)
- 构造动态的多条件的复杂的正确的查询语句所采取一江湖手段(转)
- 小小问题集锦6之---查询条件非常复杂时解决效率不高的一个办法
- ibatis条件查询(动态查询)
- Hibernate_根据条件动态组装sql/hql语句(仿ibatis动态查询语句功能)