Java Reference
In-Depth Information
It uses only a minimal amount of memory because only a subset of the data
is loaded.
■
It can reduce the load on the database since each query retrieves only a sub-
set of the rows.
■
There are a couple of drawbacks as well:
The application potentially executes the same query multiple times, which
can be inefficient.
■
The user might see an inconsistent view of the database because it could
change between queries.
■
Despite these drawbacks, this approach is the best choice for many applications,
and it is the strategy we will adopt for the example application.
11.1.2
Generating queries dynamically
Another challenge is how to dynamically generate the
SQL
SELECT
statement. An
application must typically generate
SQL
statements dynamically because there are
too many permutations of search criteria to use a static set of statements. For exam-
ple, the view orders screen allows the user to enter four search criteria, and so we
would need to write and maintain 16 (2
4
) queries, which would be pretty tedious.
Concatenate SQL fragments
One option is for the
DAO
to generate the
SELECT
statement by concatenating
fragments of
SQL
together. The
DAO
would, for example, construct the
WHERE
clause of the
SELECT
statement from the search criteria entered by the user. How-
ever, the problem with this approach is that it is not very maintainable. As well as
being messy and error prone, it is difficult to locate and change
SQL
fragments
that are embedded in code. It is also difficult to test
SQL
statements using a tool
such as
SQL
*Plus because only fragments of the
SQL
exist in the
DAO
.
Using iBATIS dynamic mapped statements
A better approach is to use the dynamic mapped statement feature of i
BATIS
. An
i
BATIS
mapped statement can include conditional tags that use the properties of a
parameter bean to determine which
SQL
fragments should be part of the
SQL
statement. This is an extremely useful feature of i
BATIS
that significantly reduces
the amount of
DAO
code. It also makes maintaining a
SQL
query easier because it
is stored in its entirety in the mapping file. Section 11.2 describes i
BATIS
dynamic
mapped statements in more detail.
Search WWH ::
Custom Search