Java Reference
In-Depth Information
Under the hood, i BATIS will run nearly the same JDBC code. i BATIS will get a
connection to the database, set the parameters, execute the statement, retrieve
the results, and close all of the resources. However, the amount of code that you
need to write is significantly reduced. Listing 2.3 shows the code needed for i BA-
TIS to run the exact same statement.
Listing 2.3
iBATIS, which is much less verbose than JDBC
<select id="getEmployee"
parameterClass="java.lang.Integer"
resultClass="Employee">
SELECT ID as id,
EMPLOYEE_NUMBER as employeeNumber,
FIRST_NAME as firstName,
LAST_NAME as lastName,
TITLE as title
FROM EMPLOYEE
WHERE EMPLOYEE_NUMBER = #empNum#
</select>
There is no comparison. The i BATIS code is more concise and easier to read,
and thus easier to maintain. We'll discuss more of the benefits of i BATIS later in
this chapter. But for now, you're probably wondering how this gets executed
from the Java code. As you've seen in earlier examples,, it's a very simple single
line of code:
Employee emp = (Employee) sqlMap.queryForObject("getEmployee",
new Integer(5));
There's nothing to it. This line of code executes the statement, sets the parame-
ters, and retrieves the results as a real Java object. The SQL is encapsulated and
externalized neatly in an Extensible Markup Language ( XML ) file. i BATIS man-
ages all of the resources behind the scenes, and the net effect is the same as the
JDBC code we saw earlier in listing 2.2.
This begs the question, does i BATIS work the same way for all systems? Is it best
suited for a particular kind of application? The next few sections will answer that,
starting with how well i BATIS works with small applications.
2.2.1
iBATIS for small, simple systems
Small applications often work with only a single database, and they often have a
fairly simple user interface and domain model. The business logic is very basic, or
Search WWH ::




Custom Search