Java Reference
In-Depth Information
deliveryInfo.put("dayOfWeek",
new Integer(dayOfWeek));
deliveryInfo.put("hour", new Integer(hour));
deliveryInfo.put("minute",
new Integer(minute));
Creates a Map
containing query
parameters
return getSqlMapClientTemplate()
.queryForList("findAvailableRestaurants",
deliveryInfo);
}
…
}
In this listing, the constructor saves the
SqlMapClientTemplate
for later by calling
setSqlMapClientTemplate()
, which is provided by the superclass. The
findAvail-
ableRestaurants()
method creates a
Map
containing the parameters for the query.
It then executes the query by invoking the
SqlMapClientTemplate
and passing it
the name of the
SQL
statement and the map containing the parameters as argu-
ments. i
BATIS
executes the
SELECT
statement and constructs a list of
Restaurant-
DTO
objects from the
ResultSet
.
In addition to
queryForList()
, the
SqlMapClientTemplate
interface provides
other methods for executing
SQL
statements, including the following:
■
insert()
executes a
SQL
INSERT
statement.
■
update()
executes a
SQL
UPDATE
statement.
Executes SELECT
statement
queryForObject()
executes a query that returns a single object.
■
Each method takes as parameters the name of the
SQL
statement to execute and
the Java object or objects that supply the
SQL
statement's parameters.
Of course, in order for i
BATIS
to do its job you must tell it three things: the
SQL
statement to execute; how to initialize its placeholders; and, if it's a query,
how to create Java objects from the result set. To do this, you must write one or
more
XML
descriptor files.
Writing the iBATIS descriptor file
i
BATIS
uses an
XML
descriptor file to define statements and result maps. A state-
ment specifies the
SQL
statement, its parameter map, and the result map to use.
The parameter map specifies the mapping between a Java object and the
SQL
statement's parameters, and the result map specifies the mapping between a
ResultSet
's columns and Java objects.
Listing 9.4 shows an excerpt of the i
BATIS
XML
file used by the
findAvail-
ableRestaurants()
method. This
XML
file defines a mapped statement, which
Search WWH ::
Custom Search