其他元素条件 IBatisNet

其他元素条件

其他元素条件有两个元素,一个为ParameterPresent,该元素检查参数对象是否存在,一个为Iterate,该元素为遍历整个集合。

(1) ParameterPresent

ParameterPresent元素属性只有prepend一个属性,表示可被覆盖的SQL语句组成部分,添加在语句的前面,为可选属性。

<isParameterPresent>

检查是否存在参数对象,即如果参数类不为NULL则查询条件有效。如:

<isParameterPresent prepend="AND">
  EMPLOYEE_TYPE = #empType#
</isParameterPresent>

<isNotParameterPresent>

检查是否不存在参数对象,如:

<isNotParameterPresent prepend="AND">
  EMPLOYEE_TYPE = 'DEFAULT'
</isNotParameterPresent>

(2) Iterate:遍历整个集合元素,为List集合中的元素重复元素体的内容。

Iterate的属性:

prepend——可被覆盖的SQL语句组成部分,添加在语句的前面,该属性为可选。

property——类型为List的用于遍历的元素属性,该属性为必选。

open——整个遍历内容体开始的字符串,用于定义括号,该属性为可选。

close ——整个遍历内容体结束的字符串,用于定义括号,该属性为可选。

conjunction——每次遍历内容之间的字符串,用于定义AND或OR,该属性为可选。

<iterate>

遍历类型为List的元素。如:

<iterate prepend="AND" property="UserNameList"

open="(" close=")" conjunction="OR">

username=#UserNameList[]#

</iterate>

注意:使用<iterate>时,在List元素名后面包括方括号[]非常重要,方括号[]将对象标记为List,以防解析器简单地将List输出成String。

Iterate元素在生成sql语句时,标签中的内容是循环生成的,如上面的例子将会生成语句:(username=xxx1 or username=xxx2 or username=xxx 3)。该元素也经常用来动态生成In查询条件,如id in (xx1,xx2,xx3,.....),括号中的(包括括号)都由该元素标签生成。

比如下面的配置例子:

<selectid="SelectSysuserDynamic4"resultMap="SysuserResult"parameterClass="System.Collections.IDictionary">

<![CDATA[ SELECT * FROM DEAN.SYSUSER ]]>

<dynamicprepend=" WHERE">

<isPropertyAvailableproperty="SEX">

<isNotNullproperty="SEX"prepend="AND">

SEX=#SEX#

</isNotNull>

</isPropertyAvailable>

<isNotNullprepend="And"property="USERIDLIST">

USERID in

<iterateproperty="USERIDLIST"open="("close=")"conjunction=",">

#USERIDLIST[]#

</iterate>

</isNotNull>

</dynamic>

</select>

原文地址:https://www.cnblogs.com/Artemisblog/p/3707204.html