Java Reference
In-Depth Information
property into the DAO layer (see chapter 10) and call the appropriate select
statement based on the parentCategoryId 's null state. Although handling this
simple example without Dynamic SQL would not have inconvenienced us that
much, the real value of Dynamic SQL becomes evident when several different
combinations of properties result in an exponential growth of statement
possibilities. By using Dynamic SQL , we increase mapped statement reuse and avoid
having to write multiple static SQL statements.
Now that we have gained a good context for the usage and power of Dynamic
SQL , let's delve deeper into exploring all the tags and their attributes.
8.2 Getting familiar with the dynamic tags
iBATIS addresses the need for Dynamic SQL through a robust set of tags which are
used to evaluate various conditions surrounding the parameter object that you
pass into your mapped statement. It is important to know the full range of tags
that exist and the various roles they play in producing correct SQL output. The
following sections break the tags into five categories: <dynamic> , binary, unary,
parameter, and <iterate> . Each grouping contains one or more related tags that
share common traits. Before we examine each of these groupings, let's take a
moment to note some common attributes and behaviors shared by all Dynamic
SQL tags.
All of the dynamic tags share the prepend , open , and close attributes. The open
and close attributes function the same in each of the tags. They unconditionally
place their values on either the beginning or the end of the resulting content of a
tag. The prepend attribute functions the same in all of the tags except the
<dynamic> tag. The <dynamic> tag will always prefix the prepend value when the
processing of its body results in content. There is no way to prevent the value from
being prefixed with the <dynamic> tag. Listing 8.2 shows some of the Dynamic SQL
tags in action.
Listing 8.2
Mock removeFirstPrepend example
<dynamic prepend="WHERE ">
<isNotEmpty property="y">
B Opening <dynamic> tag
C Simple isNotEmpty tag
D More complex
isNotEmpty tag
<isNotNull property="x" removeFirstPrepend="true"
prepend="AND" open="(" close=")">
Search WWH ::

Custom Search