Java Reference
In-Depth Information
path directory or
JAR
file and are read by i
BATIS
on startup. In section 9.5, we will
look at how to configure i
BATIS
using Spring beans.
Even though
DAO
s written using i
BATIS
are much simpler than
DAO
s written
using
JDBC
, it is important to remember that maintaining the
XML
descriptor files
and SQL statements can be a lot of work. For example, let's imagine you need to
add a new field to an object. If you are using an
ORM
framework such as Hiber-
nate or
JDO
, which maps Java objects to the database schema, you just have to add
a single entry to an
O/R
mapping file. In comparison, if you are using i
BATIS
,
which maps objects to
SQL
statements, you often need to change multiple state-
ments, including at least one
SELECT
statement, an
INSERT
statement, and an
UPDATE
statement. Keeping multiple statements in sync can be both time-consum-
ing and error-prone. However, if you must execute
SQL
statements, then i
BATIS
is
an excellent way to do that.
For more detailed information about i
BATIS
and the Spring support classes,
see
i
BATIS
in Action
[Begin, forthcoming] and
Spring in Action
[Walls 2005]. Let's
now look at how to use i
BATIS
and Spring to implement a
DAO
and see some
examples of how to use the i
BATIS
and Spring
API
s.
9.4.2
Implementing a DAO method
Most
DAO
s are simple wrappers around the database access
API
, which in this
example is i
BATIS
. Each
DAO
method performs one or more database opera-
tions—executing a query, inserting rows, updating rows, deleting rows, or calling
a stored procedure—by executing i
BATIS
mapped statements.
Because we are using test-driven development, the task of implementing the
DAO
s begins with writing a test. We could write a test that runs against the database,
but that would be slow and complicated. Instead, we will write tests that use mock
objects for the Spring and i
BATIS
API
s. Once those pass, we will write tests that run
against the database.
We are going to use the
findPendingOrder()
method to illustrate how to imple-
ment a
DAO
method with Spring and i
BATIS
. This method, which is implemented
by the
PendingOrderDAO
, retrieves the pending order, its line items, its restaurant,
and its restaurant's menu items from the database and returns a
PendingOrderDTO
containing this data.
Testing DAOs using mock objects
The first step in the process of implementing a
DAO
method is to write a mock
object test that verifies that the method executes the expected i
BATIS
statement
and returns the correct
PendingOrderDTO
. The
findPendingOrder()
method has to
execute three
SQL
statements:
Search WWH ::
Custom Search