ibatis 中isNull, isNotNull与isEmpty, isNotEmpty区别

时间:2021-08-11 21:42:38
  1. <span style="font-family: Arial, Verdana, sans-serif; white-space: normal; "><strong>isNull, isNotNull与isEmpty, isNotEmpty区别</strong></span>  

在iBATIS中isNull用于判断参数是否为Null,isNotNull相反

 

isEmpty判断参数是否为Null或者空,满足其中一个条件则其true
isNotEmpty相反,当参数既不为Null也不为空是其为true

 判断之后做相对应的表达式操作

 

如下代码:

   

 

[html]  view plain  copy
 
  1. <select id="getCustomerRegNum" resultClass="int" parameterClass="QueryCustomerCondition">  
  2.           select count(cus_id) from cus_customer_tbl   
  3.           <dynamic prepend="WHERE">  
  4.             <isNotEmpty prepend="AND" property="cusWebFrom">  
  5.                 ( CUS_CUSTOMER_TBL.CUS_WEB_FROM LIKE '%$cusWebFrom$%')  
  6.             </isNotEmpty>  
  7.             <isNotEmpty prepend="AND" property="cusWebAgent">  
  8.                 ( CUS_CUSTOMER_TBL.CUS_WEB_AGENT LIKE '%$cusWebAgent$%')  
  9.             </isNotEmpty>  
  10.           </dynamic>  
  11.    </select>  

 

 

当之传入参数 cusWebForm 而不传入 cusWebAgent 时,产生的SQL语句为:

select count(cus_id) from cus_customer_tbl    WHERE      ( CUS_CUSTOMER_TBL.CUS_WEB_FROM LIKE '%baidu%')

 

而当XML代码用<isNotNull > 配置时(注意他们的区别),

 

[html]  view plain  copy
 
  1. <select id="getCustomerRegNum" resultClass="int" parameterClass="QueryCustomerCondition">  
  2.           select count(cus_id) from cus_customer_tbl   
  3.           <dynamic prepend="WHERE">  
  4.             <isNotNull prepend="AND" property="cusWebFrom">  
  5.                 ( CUS_CUSTOMER_TBL.CUS_WEB_FROM LIKE '%$cusWebFrom$%')  
  6.             </isNotNull>  
  7.             <isNotNull prepend="AND" property="cusWebAgent">  
  8.                 ( CUS_CUSTOMER_TBL.CUS_WEB_AGENT LIKE '%$cusWebAgent$%')  
  9.             </isNotNull>  
  10.           </dynamic>  
  11.    </select>  
ibatis 中isNull, isNotNull与isEmpty, isNotEmpty区别


同样 ,当之传入参数 cusWebForm 而不传入 cusWebAgent 时,产生的SQL语句为:

 

select count(cus_id) from cus_customer_tbl     WHERE     ( CUS_CUSTOMER_TBL.CUS_WEB_FROM LIKE '%baidu%')      AND        (CUS_CUSTOMER_TBL.CUS_WEB_AGENT LIKE '%%')

 

从Sql 语句就能看出他们的区别。。。。。