-
Create ContactDAO.java, like this:
public interface ContactDAO extends DAO { public int insertContact(Contact contact); public int updateContact(Contact contact); public Contact selectContact(int contactId); public int deleteContact(int contactId); }
ContactDAO.java defines all the business methods required by a client for interacting with the
CONTACT
table. Please note that all the methods in ContactDAO.java take aContact
object as a parameter, which is a data transfer object for carrying data. -
Create a SQLMapContactDAO.java file, like this:
public class SQLMapContactDAO extends SqlMapDaoTemplate implements ContactDAO { public SQLMapContactDAO(DaoManager arg0) { super(arg0); } public int deleteContact(int contactId) { return super.delete("deleteContact", new Integer(contactId)); } public int insertContact(Contact contact) { Integer contactId =(Integer)super.insert ("insertContact",contact); return contact.getContactId(); } public Contact selectContact(int contactId) { return (Contact)super.queryForObject("getContact", new Integer(contactId)); } public int updateContact(Contact contact) { return super.update("updateContact",contact); } }
SQLMapContactDAO
is a concrete implementation of theContactDAO
interface, using SQL Maps as its persistence mechanism. Note that we are not writing any code for initializing SQL Maps, for getting a connection, or for marking a transaction boundary in our class. Instead, we extend our class from SqlMapDaoTemplate.java, which will take care of all of the underlying repetitive operations for us. Business logic is only thing that we need to worry about in ourSQLMapContactDAO
class. -
Change the
execute()
method of ContactSelectAction.java, like this:Contact contactForm = (Contact) form; Reader reader= Resources.getResourceAsReader("DAOMap.xml"); DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader); ContactDAO contactDAO = (ContactDAO) daoManager.getDao( ContactDAO.class,"sqlmap"); request.setAttribute("contactDetail", contactDAO.selectContact( contactForm.getContactId()));
The last step is changing the
execute()
method in ourContactSelectAction
class to use the DAO framework. In order to initialize the DAO framework, we need aReader
object for DAOMap.xml. The iBatis framework provides you with theResources.getResourceAsReader()
utility method that will allow you to read a resource as aReader
. Once you have aReader
object representing the DAOMap.xml file, you can pass it toDAOManagerBuilder.buildDaoManager()
. This will return an instance ofDaoManager
, which should be used for interacting with the DAO framework in the future. Ideally, you should initialize your DAO framework at application startup. In our application, we can do that by putting this code in a Struts plugin, but we are initializing it in theexecute
method just to keep our example simple.Once you have an instance of
DaoManager
, you can call thegetDao()
method with name of the interface and persistence implementation (the value of theid
attribute in the<context>
element) that you want to use. In our example, we want an instance ofSQLMapContactDAO
, so we will passContactDAO
as the name of the interface and"sqlmap"
as the persistence mechanism. Once you have an instance ofSQLMapContactDAO
, you can start calling business methods on it.
You can try this example by downloading the sample code from the Resources section on the last page of this article.