ibatis动态语法

在ibatis中使用安全的拼接语句,动态查询
ibatis比JDBC的优势之一,安全高效
说明文字在注释中

<select id="selectAllProducts" parameterClass="Product" resultMap="ProductResult">
select id,note from Product
<dynamic prepend="WHERE">
<!-- isNotNull判断参数是否存在,Integer类型 -->
<isNotNull property="id">
<!-- isGreaterThan判断参数是否大于compareValue,isGreaterEquals是大于等于 -->
<isGreaterThan prepend=" and " property="id" compareValue="0">
id = #id#
</isGreaterThan>
</isNotNull>
<!-- isNotEmpty判断字串不为空,isEmpty可以判断字串为空 -->
<isNotEmpty prepend=" and " property="note">
<!-- 模糊查询不能用#,#在是用prepareStatement的?插入参数,$是文本替换 -->
note like '%$note$%'
</isNotEmpty>
</dynamic>
</select>


用Map传参数

<select id="selectAllProducts" parameterClass="java.util.HashMap" resultMap="ProductResult">
select id,note from Product
<dynamic prepend="WHERE">
<!-- isPropertyAvailable判断属性是否有效 -->
<isPropertyAvailable property="id">
<isNotNull property="id">
<!-- isLessThan判断参数是否小于compareValue,isLessEquals是小于等于 -->
<isLessThan prepend=" and " property="id" compareValue="10">
id = #id#
</isLessThan>
</isNotNull>
</isPropertyAvailable>
</dynamic>
</select>
/*动态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>

猜你喜欢

转载自xueqi.iteye.com/blog/1765217