Java Reference
In-Depth Information
Encapsulated SQL
One of the oldest concepts in computer programming is the idea of modulariza-
tion. In a procedural application, code may be separated into files, functions, and
procedures. In an object-oriented application, code is often organized into classes
and methods. Encapsulation is a form of modularization that not only organizes
the code into cohesive modules, but also hides the implementation details while
exposing only the interface to the calling code.
This concept can be extended into our persistence layer. We can encapsulate
SQL by defining its inputs and outputs (i.e., its interface), but otherwise hide the
SQL code from the rest of the application. If you're an object-oriented software
developer, you can think of this encapsulation in the same way that you think of
separating an interface from its implementation. If you're a SQL developer, you
can think of this encapsulation much like you'd think of hiding a SQL statement
inside a stored procedure.
i BATIS uses Extensible Markup Language ( XML ) to encapsulate SQL . XML was
chosen because of its general portability across platforms, its industrywide adop-
tion, and the fact that it's more likely to live as long as SQL than any other lan-
guage and any file format. Using XML , i BATIS maps the inputs and outputs of the
statement. Most SQL statements have one or more parameters and produce some
sort of tabulated results. That is, results are organized into a series of columns and
rows. i BATIS allows you to easily map both parameters and results to properties of
objects. Consider the next example:
<select id="categoryById"
parameterClass="string" resultClass="category">
SELECT CATEGORYID, NAME, DESCRIPTION
FROM CATEGORY
WHERE CATEGORYID = #categoryId#
</select>
Notice the XML element surrounding the SQL . This is the encapsulation of the
SQL . The simple <select> element defines the name of the statement, the param-
eter input type, and the resulting output type. To an object-oriented software
developer, this is much like a method signature.
Both simplicity and consistency are achieved through externalizing and encap-
sulating the SQL . More details of the exact usage of the API and mapping syntax
will follow in chapter 2. Before we get to that, it's important to understand where
i BATIS fits in your application architecture.
Search WWH ::




Custom Search