iBatis DAO(二)

  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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值