-
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
CONTACTtable. Please note that all the methods in ContactDAO.java take aContactobject 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); } }SQLMapContactDAOis a concrete implementation of theContactDAOinterface, 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 ourSQLMapContactDAOclass. -
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 ourContactSelectActionclass to use the DAO framework. In order to initialize the DAO framework, we need aReaderobject 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 aReaderobject 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 theexecutemethod 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 theidattribute in the<context>element) that you want to use. In our example, we want an instance ofSQLMapContactDAO, so we will passContactDAOas 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.
本文介绍如何通过定义ContactDAO接口及其SQLMapContactDAO实现来操作数据库中的CONTACT表。通过Struts框架和iBatis技术,实现了数据访问层的业务逻辑处理。
265

被折叠的 条评论
为什么被折叠?



