Object-mapping tools such as Hibernate and OJB are all the rage—and rightfully so. These tools take the JDBC out of JDBC programming and make writing database access code as mundane as file I/O.
One of the latest entries into the object-mapping tool foray is the iBATIS-Database Layer. What makes iBATIS different is that it doesn't map objects to the database—it maps SQL to objects and vice versa. This simple mapping foundation makes it easy for developers with some experience programming Java and SQL to start using iBATIS.
The core of the iBATIS-Database Layer is XML files, such as:
<mapped-statement name="loadEmployee" result-class="tips.Employee">
select
emp_id as id,
last_name as lastName,
first_name as firstName,
title as title,
from employees
where emp_id = #value#
</mapped-statement>
The action of creating an Employee instance is mapped directly to a SQL statement.
The code to execute the SQL that would create your object would look something like this:
public Employee getEmployeeById(Long id) {
Employee employee = (Employee) sqlMap.executeQueryForObject("loadEmployee",
id);
}
The iBATIS-Database Layer supports mappings for each type of action that you would normally make in a database: insert, update, and delete. It also shares some advantages with other object-mapping tools such as multiple database support, data source configurable, mapping of dependent objects as properties, etc. Another advantage to using iBATIS is that there's a decidedly mild learning curve because you already know SQL, which is half of what you need to use it. However, the bad (or at least the cautionary) comes with the good.
Since you write your own SQL, you might be tempted to utilize database-specific optimizations in your SQL. Using database-specific SQL means you have to migrate your mappings over if you ever decide to switch databases.
Even with this caveat, the iBATIS-Database Layer still warrants your consideration. Take a look for yourself and see if this framework can help you.
Delivered each Thursday, our free Java newsletter provides insight and hands-on tips you need to unlock the full potential of this programming language. Automatically sign up today!



