Java Reference
In-Depth Information
AccountDao accountDao = (AccountDao)
DaoService.getDao(AccountDao.class);
So, now that you have seen an example of the goodness of using the
DAO
pattern,
you may be wondering just how to use it. First, you have to set up the configura-
tion—which is the next topic we'll discuss.
10.2 Configuring the DAO
The
dao.xml
file is the only configuration file required when using the i
BATIS
DAO
framework. It is a very simple file, and is used to provide the
DAO
manager
with the information required to manage transactions for your
DAO
classes and
tell it how to supply
DAO
implementations for your interfaces.
First, we look at the configuration elements that are available, and then exam-
ine some ways to use them to solve common problems.
10.2.1
The <properties> element
The
<properties>
element is used in the same manner as it is in the
SqlMapCon-
fig.xml
file. It is used to specify a properties file, and any properties listed in it are
available for use in the configuration of the
DAO
layer using the
${name}
syntax.
This approach is very useful in cases where you have separate servers for devel-
opment and production (and potentially staging and testing servers as well). In
these cases, you can configure all of your
DAO
classes and put just the items that
differ by environment into the properties file. Then, when you deploy to each
environment, all that has to be changed is the properties file.
10.2.2
The <context> element
A
DAO
context is a grouping of related configuration information and
DAO
implementations:
<context id="example">
Usually a context is associated with a single data source such as a relational data-
base or a flat file. By configuring multiple contexts, you can easily centralize
access configuration to multiple databases.
In section 10.3, we look at using contexts to create multiple
DAO
groups that
employ different data access models—one that uses
SQL
maps (of course!), one
that uses Hibernate, and finally, one that uses straight
JDBC
.
Each context has its own transaction manager and set of
DAO
implementations.
In the next two sections, you will learn how to configure each of those items.



