首页 > 代码库 > 在ibatis中isNotNull和isNotEmpty的区别

在ibatis中isNotNull和isNotEmpty的区别

 

Unary tags examine the state of a bean property and do not perform comparisons
against any other values. The body content is included if the result of the state is
true. All unary tags share the property attribute. The property attribute is used to
specify the property on the parameter object that will be used to examine the
state. The name of the tag indicates the type of state that is being examined. The
unary tag attributes are shown in table 8.4.
All of the attributes in table 8.4 are available in the unary dynamic SQL tags listed
in table 8.5.
Table 8.4 Unary tag attributes
property
(required)
The property of the parameter used for state comparison.
prepend
(optional)
This value is used to prepend to the tag’s resulting body content. The prepend
value will not be prepended (a) when the tag’s resulting body content is empty; (b) if
the tag is the first to produce body content and is nested in a tag with the remove-
FirstPrepend attribute set to true; or (c) if the tag is the first to produce body content
following a <dynamic> tag with a prepend attribute value that is not empty.
open
(optional)
This value is used to prefix to the tag’s resulting body content. The open value will
not be prefixed if the tag’s resulting body content is empty. The open value is prefixed
before the prepend attribute’s value is prefixed. For example, if prepend="OR
" and open="(", then the resulting combined prefix would be "OR (".
close
(optional)
This value is used to append to the tag’s resulting body content. The append value
will not be appended if the tag’s resulting body content is empty.
removeFirst-
Prepend
(optional)
This attribute value defines whether the first nested content-producing tag will have
its prepend value removed.
Table 8.5 Unary tags
<isProperty-
Available>
Determines whether the specified property exists in the parameter. With a
bean, it looks for a property. With a map, it looks for a key.
<isNotProperty-
Available>
Checks whether the specified property does not exist in the parameter. With
a bean, it looks for a property. With a map, it looks for a key.
<isNull> Determines whether the specified property is null. With a bean, it looks at
the value of the property getter. With a map, it looks for a key. If the key
does not exist, it will return true.
<isNotNull> Determines whether the specified property is anything other than null. With
a bean, it looks at the value of the property getter. With a map, it looks for a
key. If the key does not exist, it will return false.

 <isEmpty> Determines whether the specified property is a null or empty String, Collection,
or String.valueOf().
<isNotEmpty> Determines whether the specified property is not a null or empty String,
Collection, or String.valueOf().

 

 

 

 

 

 

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------------

 

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

 

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

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

 

如下代码:

  

 

[html] view plaincopyprint?
  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
	 <select id="getCustomerRegNum" resultClass="int" parameterClass="QueryCustomerCondition">            select count(cus_id) from cus_customer_tbl             <dynamic prepend="WHERE">            	<isNotEmpty prepend="AND" property="cusWebFrom">            		( CUS_CUSTOMER_TBL.CUS_WEB_FROM LIKE ‘%$cusWebFrom$%‘)            	</isNotEmpty>            	<isNotEmpty prepend="AND" property="cusWebAgent">            		( CUS_CUSTOMER_TBL.CUS_WEB_AGENT LIKE ‘%$cusWebAgent$%‘)            	</isNotEmpty>            </dynamic>     </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 plaincopyprint?
  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
	 <select id="getCustomerRegNum" resultClass="int" parameterClass="QueryCustomerCondition">            select count(cus_id) from cus_customer_tbl             <dynamic prepend="WHERE">            	<isNotNull prepend="AND" property="cusWebFrom">            		( CUS_CUSTOMER_TBL.CUS_WEB_FROM LIKE ‘%$cusWebFrom$%‘)            	</isNotNull>            	<isNotNull prepend="AND" property="cusWebAgent">            		( CUS_CUSTOMER_TBL.CUS_WEB_AGENT LIKE ‘%$cusWebAgent$%‘)            	</isNotNull>            </dynamic>     </select>

同样 ,当之传入参数 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 语句就能看出他们的区别。。。。。

在ibatis中isNotNull和isNotEmpty的区别