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. Then you'd create separate classes containing persistence logic for each method. 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. This relationship is shown in Figure 1.

ContactDAO interface and implementations
Figure 1. ContactDAO interface and implementations

Related Reading

J2EE Design Patterns
By William Crawford, Jonathan Kaplan

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.

This article is a step-by-step guide on how to use the iBatis DAO framework in your application. We will start by looking at how to change our sample application developed in the SQL Maps article to use the DAO framework. Then we will talk about the architecture of the DAO framework. Next we will look at which transaction managers are supported in DAO framework, and will finish with a section on how to create your own transaction manager.

Sample Application

We will start by changing our sample application developed in the SQL Maps article to use the DAO framework.

  1. Copy the ibatis-dao-2.jar file to your WEB-INF/lib folder.

  2. Create DAOMap.xml at the root of your JavaSource folder, like this:

    
    <daoConfig>
     <context id="sqlmap">
      <transactionManager type="SQLMAP">
      <property name="SqlMapConfigResource" value=
       "com/sample/contact/dao/sqlmap/SqlMapConfig.xml"/>
      </transactionManager>
      <dao interface="com.sample.contact.dao.ContactDAO"
       implementation=
       "com.sample.contact.dao.sqlmap.SQLMapContactDAO"/>
     </context>
    </daoConfig>
    
    

    DAOMap.xml file is a deployment descriptor for your iBatis DAO framework. <daoConfig> is the root element. Each <context> element represents one persistence mechanism. In our example, we will use only SQL Maps for persistence, so we so have only one <context> element. Every persistence mechanism should have one <transactionManager> element, which represents the manager used for getting a connection to the underlying data store and marking the transaction boundary. We will talk more about transactionManager later.

    The <context> element also contains a list of DAOs for the given particular persistence mechanism. In our example, we want to create a ContactDAO, which will use SQL Maps for persistence, so we will add one <dao> element defining SQLMapContactDAO.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值