ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

iBatis DAO
Pages: 1, 2, 3, 4, 5

  1. 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 a Contact object as a parameter, which is a data transfer object for carrying data.

  2. 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 the ContactDAO 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 our SQLMapContactDAO class.
  3. 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 our ContactSelectAction class to use the DAO framework. In order to initialize the DAO framework, we need a Reader object for DAOMap.xml. The iBatis framework provides you with the Resources.getResourceAsReader() utility method that will allow you to read a resource as a Reader. Once you have a Reader object representing the DAOMap.xml file, you can pass it to DAOManagerBuilder.buildDaoManager(). This will return an instance of DaoManager, 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 the execute method just to keep our example simple.

    Once you have an instance of DaoManager, you can call the getDao() method with name of the interface and persistence implementation (the value of the id attribute in the <context> element) that you want to use. In our example, we want an instance of SQLMapContactDAO, so we will pass ContactDAO as the name of the interface and "sqlmap" as the persistence mechanism. Once you have an instance of SQLMapContactDAO, 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.

Pages: 1, 2, 3, 4, 5

Next Pagearrow