Java Reference
In-Depth Information
</sql>
<sql id="where-shipped-after-value">
<![CDATA[
where shipDate > #value:DATE#
]]>
</sql>
Includes orders
shipped after date
D
<select
id="getOrderShippedAfter"
resultClass="map">
<include refid="select-order" />
<include refid="where-shipped-after-value" />
</select>
Gets all columns for or-
ders shipped after date
E
<select
id="getOrderCountShippedAfter"
resultClass="int">
<include refid="select-count" />
<include refid="where-shipped-after-value" />
</select>
Gets count of orders
shipped after date
F
While listing 4.1 is a fairly trivial example, it shows the technique clearly without
making it overly complex. We define three SQL fragments: one to get all the columns
from a table , another to get a count of the rows returned from a query , and
one more for the condition to use to filter that data. We then build two mapped
statements that return the full objects, getOrderShippedAfter , and the count,
getOrderCountShippedAfter , using those fragments. In this case it would have
been easier to just duplicate the SQL code, but the process of duplication becomes
much more error-prone when you start to use more complex operations or dynamic
SQL (see chapter 8 for more on Dynamic SQL use) in a SQL fragment.
b
C
D
E
F
4.2 Using <select> mapped statements
Selecting data from a database is one of the most fundamental uses of an applica-
tion. The i BATIS framework makes most SELECT statements effortless, and pro-
vides many features to make it possible to access virtually any data you want out of
your database.
4.2.1
Using inline parameters with the # placeholders
So far, all of the previous examples have been unrealistically simple, because rarely
do you want to execute a query without any selection criteria. Inline parameters are
Search WWH ::




Custom Search