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, nice tip

    ReplyDelete
  2. 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

Live - solving the jasper report out of memory and high cpu usage problems

I still can not find the solution. So I summary all the things and tell my boss about it. If any one knows the solution, please let me know. Symptom: 1.        The JVM became Out of memory when creating big consumption report 2.        Those JRTemplateElement-instances is still there occupied even if I logged out the system Reason:         1. There is a large number of JRTemplateElement-instances cached in the memory 2.     The clearobjects() method in ReportThread class has not been triggered when logging out Action I tried:      About the Virtualizer: 1.     Replacing the JRSwapFileVirtualizer with JRFileVirtualizer 2.     Not use any FileVirtualizer for cache the report in the hard disk Result: The japserreport still creating the a large number of JRTemplateElement-instances in the memory        About the work around below,      I tried: item 3(in below work around list) – result: it helps to reduce  the size of the JRTemplateElement Object        

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

Data Source / Query Executer net.sf.jasperreports.csv.column.names.{arbitrary_name} net.sf.jasperreports.csv.date.pattern net.sf.jasperreports.csv.encoding net.sf.jasperreports.csv.field.delimiter net.sf.jasperreports.csv.locale.code net.sf.jasperreports.csv.number.pattern net.sf.jasperreports.csv.record.delimiter net.sf.jasperreports.csv.source net.sf.jasperreports.csv.timezone.id net.sf.jasperreports.ejbql.query.hint.{hint} net.sf.jasperreports.ejbql.query.page.size net.sf.jasperreports.hql.clear.cache net.sf.jasperreports.hql.field.mapping.descriptions net.sf.jasperreports.hql.query.list.page.size net.sf.jasperreports.hql.query.run.type net.sf.jasperreports.jdbc.concurrency net.sf.jasperreports.jdbc.fetch.size net.sf.jasperreports.jdbc.holdability net.sf.jasperreports.jdbc.max.field.size net.sf.jasperreports.jdbc.result.set.type net.sf.jasperreports.query.chunk.token.separators net.sf.jasperreports.query.executer.factory.{language} net.sf.jasperreports.xpath.