iBatis DAO(五)

Developing an XML Transaction Map

There may be situations when you want to read and write data from an XML file instead of an RDBMS. For example, imagine working on a banking project where you don't have direct access to bank's database, and instead the customer provides you with sample data in the form of XML files. Your requirement is that your application should use these XML files during development, and a RDBMS in production. We will take our contact application as example and change it so that it will read and write data from an XML file instead of an RDBMS. This requires two changes:

  1. Add support for an external transactionManager in your DAOMap.xml file.
  2. Create a XMLContactDAO.java file, as follows:
    
    public class XMLContactDAO implements ContactDAO {
     public static final String
      CONTACTXMLNAME = "c://Contact.xml";
     public XMLContactDAO(DaoManager manager) {
        super(manager);
     }
     public int insertContact(Contact contact) {
      HashMap contactMap = loadChanges();
      if (contactMap.get(new Integer
        (contact.getContactId())) == null)
       contactMap.put(new
        Integer(contact.getContactId()), contact);
      saveChanges(contactMap);
      return contact.getContactId();
     }
     public Contact selectContact(int contactId) {
      HashMap contactMap = loadChanges();
      return (Contact) contactMap.get(
       new Integer(contactId));
     }
     public HashMap loadChanges() {
      HashMap contactMap = null;
      try {
       XStream xstream = new XStream(new DomDriver());
       xstream.alias("contact", Contact.class);
       contactMap =
        (HashMap) xstream.fromXML(
         new FileReader(CONTACTXMLNAME),HashMap.class);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
        return new HashMap();
      }
      return contactMap;
     }
     public void saveChanges(HashMap contactMap) {
      try {
       XStream xstream = new XStream();
       xstream.alias("contact", Contact.class);
       xstream.toXML(contactMap,
        new FileWriter(CONTACTXMLNAME));
       } catch (IOException e) {
       e.printStackTrace();
      }
     }
    }
    
    

In this example, XMLContactDAO implements the ContactDAO business interface. Since we are using an EXTERNAL transaction manager, we cannot use any of the existing Template classes. In our class, we have created two simple methods--loadChanges() and saveChanges--for reading and writing an XML file using the XStream framework. XStream is open source framework that helps you read an XML file as a Java object and save a Java object as an XML file.

Conclusion

Nowadays, a lot of new persistence frameworks are coming up. As a developer, this is both good and bad for you. It's good because now you have many more options to choose from. But it's bad because you have to make choices. And the bigger problem is that you have to commit to one persistence framework at the start of the project, at which point you may not be completely clear on project requirements or completely sure if a particular framework can fulfill all your requirements. DAO is a very easy-to-use and useful framework that guards against changes in persistence mechanisms. It may look like you're making some investment up front, but it will definitely help you in the long run.

Resources

Sunil Patil has worked on J2EE technologies for more than five years. His areas of interest include object relational mapping tools, UI frameworks, and portals.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值