iBatis DAO

The Data Access Object pattern in the Core J2EE Pattern Catalog suggests that while creating a robust J2EE application, you should abstract and encapsulate all access to a data source behind a common API. In terms of programming, what this means is to create an interface defining all of the business methods that your application needs. Inside your application, you use this interface when you want to interact with your data store, and then create a separate class that implements this interface and has the logic for interacting with that particular data store.

For example, consider the sample application developed in the article iBatis: SQL Maps. This is a Struts application that allows you to fire SELECT, INSERT, UPDATE and DELETE SQL queries on a CONTACT table. In this application, we are using SQL Maps as the persistence framework. Now say we want to change this application so that the CONTACT table is stored in an XML file instead of a RDBMS, or want to use Hibernate for SELECT queries and SQL Map for the other three queries, perhaps because Hibernate provides better support for caching. This will be very hard to implement, and if we are able to change it at all, it won’t be a very clean solution.

A better approach for this type of problem would be to create a ContactDAO interface and define business methods for SELECT, INSERT, UPDATE, and DELETE queries in this interface.So there would be a class that knows how to interact with the CONTACT table using SQL Maps, another class that knows how to interact with the CONTACT table if it is stored in a XML file instead of a RDBMS, and so on. Inside your application, you’d choose from different implementations of ContactDAO, depending on your requirements.

READ  How to check performance in J2EE applications

 

 

iBatis Data Access Object (DAO) is an open source framework now hosted by Apache and targeted at solving these type of problems. It provides you with an infrastructure for creating applications based on the DAO pattern. What this means is that you can create a XML file and declare that XMLContactDAO.java is the implementation class for ContactDAO, which knows how to read and write contacts in an XML file instead of a RDBMS. SQLMapContactDAO is an implementation class that knows how to interact with the CONTACT table using SQL Maps as its persistence framework. Inside your application, you ask the DAO framework for an implementation of ContactDAO for XML, and it will provide you with an XMLContactDAO object. Also, the DAO framework provides you with a uniform interface to handle transaction management, irrespective of what persistence mechanism you use. It will also take care of the low-level details of connection management and initializing persistence frameworks.