ibatis中isEquals、isNotEmpty的用法

时间:2021-08-01 21:41:39

一:isEquals用于动态拼接sql

如下实例:

如果status的状态为0,则更新attribute1;状态为1,则更新attribute2;

状态为2,则更新attribute3。

 1 <update   id="topup.doEffect" parameterClass="java.util.HashMap">
 2 update  cis_customer  set  code_id  = #codeId# ,
 3 <isEqual property="status" compareValue="0" prepend=",">  
 4             attribute1=#attribute1#</isEqual>  
 5         <isEqual property="status" compareValue="1" prepend=",">  
 6             attribute2=#attribute2#</isEqual>  
 7         <isEqual property="status" compareValue="2" prepend=",">  
 8             attribute3=#attribute3#</isEqual>
 9 where  id = #id#
10 </update>

二:isNotEmpty

如下实例:

查询cis_customer这张表,如果属性aaa不为空,则将条件aaa=#aaa#拼接;

如果bbb不为空,则将条件bbb=#bbb#拼接;如果属性ccc不为空,则将条件ccc=#ccc#拼接;

 

 1 <select  id="topup.querySuccessCount" parameterClass="java.util.HashMap">
 2      select id,name from cis_customer where 1=1
 3      <isNotEmpty  property="aaa"  prepend="and">  
 4                  aaa=#aaa#
 5   </isNotEmpty  >  
 6   <isNotEmpty  property="bbb"  prepend="and">  
 7                  bbb=#bbb#
 8    </isNotEmpty  >  
 9    <isNotEmpty  property="ccc" prepend="and">  
10                 ccc=#ccc#
11   </isNotEmpty  >
12   </select>