Java Reference
In-Depth Information
which has
startingIndex
and
pageSize
parameters that specify which page of the
result set to return, as well as a
OrderSearchCriteria
parameter that contains the
search criteria entered by the user and the selected sort order. It has properties
that represent the order number, phone number, email address, and date range.
The
findOrders()
method returns a
PagedQueryResult
object, which contains the
list of
OrderSummary
DTO
objects, and a boolean flag indicating whether there are
more orders.
OrderDAOIBatisImpl
, which implements the
OrderDAO
interface, calls
SqlMap-
ClientTemplate
to generate and execute the
findOrders
mapped statement,
which is defined in the Order.xml descriptor file.
findOrders
is an i
BATIS
dynamic
mapped statement. It contains conditional
XML
elements that use the values from
the
OrderSearchCriteria
JavaBean to determine whether to include one or more
SQL
fragments in the statement. Using this mechanism to dynamically generate
SQL
queries significantly simplifies the
DAO
by eliminating conditional logic and
code that concatenates
SQL
fragments. In fact, because i
BATIS
does all of the
work,
findOrders()
consists of a few lines of code that call
SqlMapClientTem-
plate.queryForList()
, which executes the query and returns a list of beans con-
structed from the result set.
We have seen earlier that, depending on the query, we will want to select a page
of results by either navigating through the result set or by executing a
SELECT
state-
ment that uses a
ROWNUM
-like feature. Accordingly, i
BATIS
defines two versions of
the
queryForList()
method. One version takes parameters that specify the range
of rows to return. It navigates through the
JDBC
result to extract the specified rows.
The other version of
queryForList()
returns a list containing all of the rows found
by the query. It's useful when the
SQL
query returns just the required rows by using
a
ROWNUM
-like feature. Let's look at how to use each of these methods.
11.2.1
Using queryForList() to select the rows
The first version of
queryListForList()
that we will examine is the one that takes
parameters specifying the range of rows to return. Here is the implementation
of the
OrderDAO.findOrders()
method that executes a mapped statement using
this version:
public class OrderDAOIBatisImpl extends SqlMapClientDaoSupport
implements OrderDAO {
public PagedQueryResult findOrders(int startingIndex,
int pageSize, OrderSearchCriteria criteria) {
Map map = new HashMap();
map.put("pageSize",
new Integer(pageSize + startingIndex + 1));
map.put("criteria", criteria);
Search WWH ::
Custom Search