Skip to main content

Learning iBATIS - Using Dynamic SQL

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>








Comments

  1. Thanks. great to hear you like the tip. I haven't use iBatis for a while:)

    ReplyDelete

Post a Comment

Popular posts from this blog

Stretch a row if data overflows in jasper reports

It is very common that some columns of the report need to stretch to show all the content in that column. But  if you just specify the property " stretch with overflow' to that column(we called text field in jasper report world) , it will just stretch that column and won't change other columns, so the row could be ridiculous. Haven't find the solution from internet yet. So I just review the properties in iReport one by one and find two useful properties(the bold highlighted in example below) which resolve the problems.   example:
<band height="20" splitType="Stretch"> <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true"> <reportElement stretchType="RelativeToTallestObject" mode="Opaque" x="192" y="0" width="183" height="20"/> <box leftPadding="2"> <pen lineWidth="0.25"/> …

JasperReports - Configuration Reference

Spring - Operations with jdbcTemplate

This class manages all the database communication and exception handling using a java.sql.Connection that is obtained from the provided DataSource. JdbcTemplate is a stateless and threadsafe class and you can safely instantiate a single instance to be used for each DAO.


Use of Callback Methods
JdbcTemplate is based on a template style of programming common to many other parts of Spring. Some method calls are handled entirely by the JdbcTemplate, while others require the calling class to provide callback methods that contain the implementation for parts of the JDBC workflow. This is another form of Inversion of Control. Your application code hands over the responsibility of managing the database access to the template class. The template class in turn calls back to your application code when it needs some detail processing filled in. These callback methods are allowed to throw a java.sql.SQLException, since the framework will be able to catch this exception and use its built-in excepti…