More than anything else, iBATIS is an alternative to writing JDBC and ADO.NET code.APIs like JDBC and ADO.NET are powerful, but tend to be verbose and repetitive. Consider the JDBC example below:
public Employee getEmployee (int id) throws SQLException {
Employee employee = null;
String sql = "SELECT * FROM EMPLOYEE " +
"WHERE EMPLOYEE_NUMBER = ?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
employee = null;
while (rs.next()) {
employee = new Employee();
employee.setId(rs.getInt("ID"));
employee.setEmployeeNumber(rs.getInt("EMPLOYEE_NUMBER"));
employee.setFirstName(rs.getString("FIRST_NAME"));
employee.setLastName(rs.getString("LAST_NAME"));
employee.setTitle(rs.getString("TITLE"));
}
} finally {
try {
if (rs != null) rs.close();
} finally {
try {
if (ps != null) ps.close();
} finally {
if (conn != null) conn.close();
}
}
}
return employee;
}
It’s easy to see the overhead created by the JDBC API. Every line is necessary, though, so there’s no easy way to reduce it. At best, a few of the lines can be extracted into utility methods, most notably the closing of resources such as the PreparedStatement and the ResultSet.
 
Under the hood, iBATIS will run nearly the same JDBC code. iBATIS will get a connection to the database, set the parameters, execute the statement, retrieve the results, and close all of the resources. Consider the example below:
 
 
< 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 iBATIS code is more concise and easier to read, and thus easier to maintain.A sample tutorial to execute a DB statement in iBATIS is mentioned below:
 
 
Employee emp = (Employee) sqlMap.queryForObject("getEmployee",
new Integer(5));

Related Posts:


Enter your email address:

Delivered by FeedBurner



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)