Java Reference
In-Depth Information
11.3.2
Why use Spring instead of iBATIS?
This is a fair question to ask—this is a topic about i
BATIS
, so why are we talking
about using something else for the
DAO
layer? Both Spring and i
BATIS
have their
advantages and disadvantages, and the decision requires that you have an under-
standing of those pros and cons as well as the needs of your application.
The advantage of the i
BATIS
DAO
layer is that it is a quick and easy solution. If
you have the i
BATIS
SQL
Map download, you have the i
BATIS
DAO
framework,
too. It is a much simpler framework than Spring if all you need is transaction and
connection management. In that case, the i
BATIS
DAO
layer is probably adequate
for your application.
The simplicity of the i
BATIS
DAO
is also its biggest disadvantage: once you
begin using the
DAO
pattern, and start taking advantage of the testability that
decoupling provides, you will want to use the same approach in different areas of
your application.
For example, in a Struts application we will use the same approach with our
Action
class and our business logic class that we use between our business logic
class and our
DAO
class. Instead of the code knowing the implementations it
needs, it only knows the interfaces that it needs, and the implementations are
plugged in through configuration. This keeps the
Action
classes simple, and
makes every layer easier to test.
In addition to being able to manage that separation, Spring can be used to
manage your connections and transactions, just like the i
BATIS
DAO
layer does.
The big advantage of Spring is that it is not only for
DAO
classes but for all seg-
ments of your application.
11.4 Creating your own DAO layer
Sometimes, neither the i
BATIS
DAO
support nor the Spring
DAO
support is
exactly what you need. In those cases, you need to “roll your own”
DAO
layer.
Creating a
DAO
layer from scratch may sound like a daunting task. However,
you could be surprised, because it is actually a fairly straightforward pattern to
implement. There are essentially three tiers to an effective
DAO
layer:
Separate interface from implementation.
1
Decouple implementation with an externally configured factory.
2
Provide transaction and connection management.
3


