ibatis多條件查詢

查詢

<select id="searchUserByParam"

parameterClass="User"
resultClass="User">
select * from User

<dynamic prepend="WHERE">

<!-- isNotNull 判斷NULL,isNotEmpty判斷空字符串 -->

<isNotNull property="city">
city=#city#

</isNotNull>

<!-- <isNotEmpty property="cityId">判斷NULL,isNotEmpty判斷空字符串 -->

<isNotNull property="createTime">
createTime=#createTime#
</isNotNull>
<isNotEmpty prepend="OR" property="locationCity">
locationCity like '%$locationCity$%' or locationCity IS NULL
</isNotEmpty>
<!-- isNotEmpty判斷字串不爲空,isEmpty可以判斷字串爲空 --> 
<isNotEmpty prepend="AND" property="position">
replace(position,' ','' )  like replace('%$position$%',' ','')
</isNotEmpty>
<isNotNull property="uId">
<isGreaterThan prepend="AND" property="uId" compareValue="0">
uId!=#uId#
</isGreaterThan>
</isNotNull>
</dynamic>
ORDER BY id desc limit #begin#,#end#

</select>


------

條件

age字段大於18, compareValue 比較值

<isGreaterThan prepend="and" property="age" compareValue="18">  


<isGreaterThan>  比較屬性值是否大於靜態值或另一個屬性值。
<isGreateEqual> 比較屬性值是否大於等於靜態值或另一個屬性值。  
<isEqual> 比較屬性值和靜態值或另一個屬性值是否相等。
<isNotEqual> 比較屬性值和靜態值或另一個屬性值是否不相等。
<isLessThan> 比較屬性值是否小於靜態值或另一個屬性值。
<isLessEqual> 比較屬性值是否小於等於靜態值或另一個屬性值。


isNotEmpty:

<dynamic   prepend= "where ">
   <isNotEmpty prepend="and" property="addr">
      <![CDATA[
     addr=$addr$
   ]]>
      </isNotEmpty>

<isNotEmpty>意思則爲當次條件不爲空時執行其中語句 prepend="" 依賴約束, 值可以是 AND 也可以是OR  property="" 就是對於這個條件所判定的取值字段 例如"addr"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章