In SQL, an equal sign(=) cannot be used to compare null equalities. The IS keyword is needed to successfully test for the equality with a NULL. Since we want to use the same SQL statement to handle both NULL and non-NULL comparisons, we will use Dynamic SQL to accomplish this with one mapped statement. For example,
<select id="getChildCaategories" parameterClass="Category" resultClass="Category">
select * from category
<dynamic prepend=" Where ">
<isNull property="parentCategoryId">
parentCategoryID is NULL
</isNull>
<isNotNull property="parentCategoryId">
parentCategoryId=#parentCategoryId#
</isNotNull>
</dynamic>
</select>
# The <dynamic> tag
prepend(optional): This value is used to prepend to the tag's resulting body content. The prepend value will not be prepended when the tag's resulting body content is 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 is empty. The open value is prefixed before the prepend attribue's value is prefixed. For example, if prepend=" WHEN' and open="(" , then the resulting combined prefix would be "WHEN (".
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.
removeForst-Prepend(optional): This attribue value defines whether the first nested content-producing tag will have its prepend value removed.
another example,
<select id="getShippingType" parameterClass="Cart"
resultClass="Shipping">
SELECT * FROM Shipping
<dynamic prepend="WHERE ">
<isGreaterEqual property="weight" compareValue="100">
shippingType='FREIGHT'
</isEqual>
<isLessThan property="weight" compareValue="100">
shippingType='STANDARD'
</isLessThan>
</dynamic>
</select>
iBATIS binary dynamic tags
<isEqual> Compares the property attribute with compareProperty or compareValue to
determine if they are equal
<isNotEqual> Compares the property attribute with compareProperty or compareValue to
determine if they are equal
<isGreaterThan> Determines whether the property attribute is greater than compareProperty or
compareValue
<isGreaterEqual>
Determines whether the property attribute is greater than or equal to compare-
Property or compareValue
<isLessThan> Determines whether the property attribute is less than compareProperty or
compareValue
<isLessEqual> Determines whether the property attribute is less than or equal to compare-
Property or compareValue
example 3:
<select id="getProducts" resultClass="Product">
SELECT * FROM Products
<isParameterPresent prepend="WHERE ">
<isNotEmpty property="productType">
productType=#productType#
</isNotEmpty>
</ isParameterPresent >
</select>
Parameter tags:
<isParameterPresent>: Determines whether a parameter object is present
<isNotParameterPresent>: Determines whether a parameter object does not exist
example 4:
<select id="getProducts" parameterClass="Product"
resultClass="Product">
SELECT * FROM Products
<dynamic prepend="WHERE productType IN ">
<iterate property="productTypes"
open="(" close=")"
conjunction=",">
productType=#productType#
</iterate>
</dynamic>
</select>
The <iterate> tag
The <iterate> tag takes a property that is a Collection or array to produce repetitive portions of SQL from a set of values. The list is rendered by rendering the values of the list to a SQL fragment separated by the conjunction attributes' value.
Example 5:
<select id="searchProductsWithProductSearch"
parameterClass="productSearch"
resultClass="product" >
SELECT DISTINCT
p.PRODUCTID,
p.NAME,
p.DESCRIPTION,
p.IMAGE,
p.CATEGORYID
FROM Product p
<isEqual property="itemProperties" compareValue="true">
INNER JOIN Item i ON i.productId=p.productId
</isEqual>
<dynamic prepend="WHERE">
iterate
property="categoryIds"
open="p.categoryId IN (" close=")"
conjunction="," prepend="BOGUS">
#categoryIds[]#
</iterate>
<isNotEmpty property="productName" prepend="AND">
p.name LIKE #productName#
</isNotEmpty>
<isNotEmpty property="productDescription" prepend="AND">
p.description LIKE #productDescription#
</isNotEmpty>
<isNotEmpty property="itemName" prepend="AND">
i.name LIKE #itemName#
</isNotEmpty>
<isNotEmpty property="itemDescription" prepend="AND">
i.description LIKE #itemDescription#
</isNotEmpty>
</dynamic>
</select>
<select id="getChildCaategories" parameterClass="Category" resultClass="Category">
select * from category
<dynamic prepend=" Where ">
<isNull property="parentCategoryId">
parentCategoryID is NULL
</isNull>
<isNotNull property="parentCategoryId">
parentCategoryId=#parentCategoryId#
</isNotNull>
</dynamic>
</select>
# The <dynamic> tag
prepend(optional): This value is used to prepend to the tag's resulting body content. The prepend value will not be prepended when the tag's resulting body content is 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 is empty. The open value is prefixed before the prepend attribue's value is prefixed. For example, if prepend=" WHEN' and open="(" , then the resulting combined prefix would be "WHEN (".
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.
removeForst-Prepend(optional): This attribue value defines whether the first nested content-producing tag will have its prepend value removed.
another example,
<select id="getShippingType" parameterClass="Cart"
resultClass="Shipping">
SELECT * FROM Shipping
<dynamic prepend="WHERE ">
<isGreaterEqual property="weight" compareValue="100">
shippingType='FREIGHT'
</isEqual>
<isLessThan property="weight" compareValue="100">
shippingType='STANDARD'
</isLessThan>
</dynamic>
</select>
iBATIS binary dynamic tags
<isEqual> Compares the property attribute with compareProperty or compareValue to
determine if they are equal
<isNotEqual> Compares the property attribute with compareProperty or compareValue to
determine if they are equal
<isGreaterThan> Determines whether the property attribute is greater than compareProperty or
compareValue
<isGreaterEqual>
Determines whether the property attribute is greater than or equal to compare-
Property or compareValue
<isLessThan> Determines whether the property attribute is less than compareProperty or
compareValue
<isLessEqual> Determines whether the property attribute is less than or equal to compare-
Property or compareValue
example 3:
<select id="getProducts" resultClass="Product">
SELECT * FROM Products
<isParameterPresent prepend="WHERE ">
<isNotEmpty property="productType">
productType=#productType#
</isNotEmpty>
</ isParameterPresent >
</select>
Parameter tags:
<isParameterPresent>: Determines whether a parameter object is present
<isNotParameterPresent>: Determines whether a parameter object does not exist
example 4:
<select id="getProducts" parameterClass="Product"
resultClass="Product">
SELECT * FROM Products
<dynamic prepend="WHERE productType IN ">
<iterate property="productTypes"
open="(" close=")"
conjunction=",">
productType=#productType#
</iterate>
</dynamic>
</select>
The <iterate> tag
The <iterate> tag takes a property that is a Collection or array to produce repetitive portions of SQL from a set of values. The list is rendered by rendering the values of the list to a SQL fragment separated by the conjunction attributes' value.
Example 5:
<select id="searchProductsWithProductSearch"
parameterClass="productSearch"
resultClass="product" >
SELECT DISTINCT
p.PRODUCTID,
p.NAME,
p.DESCRIPTION,
p.IMAGE,
p.CATEGORYID
FROM Product p
<isEqual property="itemProperties" compareValue="true">
INNER JOIN Item i ON i.productId=p.productId
</isEqual>
<dynamic prepend="WHERE">
iterate
property="categoryIds"
open="p.categoryId IN (" close=")"
conjunction="," prepend="BOGUS">
#categoryIds[]#
</iterate>
<isNotEmpty property="productName" prepend="AND">
p.name LIKE #productName#
</isNotEmpty>
<isNotEmpty property="productDescription" prepend="AND">
p.description LIKE #productDescription#
</isNotEmpty>
<isNotEmpty property="itemName" prepend="AND">
i.name LIKE #itemName#
</isNotEmpty>
<isNotEmpty property="itemDescription" prepend="AND">
i.description LIKE #itemDescription#
</isNotEmpty>
</dynamic>
</select>
Thanks, nice tip
ReplyDeleteThanks. great to hear you like the tip. I haven't use iBatis for a while:)
ReplyDelete