iBatis DAO
Pages: 1, 2, 3, 4, 5
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:
- Add support for an external
transactionManagerin your DAOMap.xml file. - 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
- Sample code for this article
- iBatis home page
- " Core J2EE Patterns: Data Access Object"
- Hibernate home page (see also CodeZoo: Hibernate)
- "Serializing Java Objects with XStream"
- XStream (see also CodeZoo: XStream)
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.
Return to ONJava.com.
-
iBATIS2.x
2005-09-30 11:14:45 Venky7357 [View]
-
iBATIS
2010-03-02 06:06:32 Trinath.T [View]
-
Why extend Dao?
2005-09-26 12:48:41 adamkrieg [View]
-
Why extend Dao?
2006-07-08 18:45:19 Fucema [View]
-
Why extend Dao?
2005-09-27 06:08:10 SunilPatil [View]
-
EXTERNAL transactionmanager - getting properties
2005-08-26 06:01:48 thomasborgsalling [View]
- Trackback from http://gleamynode.net/wordpress/archives/72
iBatis DAO
2005-08-15 16:11:23 [View]
-
Transactions
2005-08-13 08:04:28 laker2000 [View]
-
Transactions
2005-08-16 21:46:43 SunilPatil [View]
-
Talk about timely!!!
2005-08-11 14:57:25 Demetrius- [View]
-
Talk about timely!!!
2005-08-11 14:54:22 Demetrius- [View]
-
Spring?
2005-08-11 11:51:25 stnfdnacho [View]
-
Good article
2005-08-10 17:15:46 whackity [View]
-
Figures
2005-08-10 17:13:43 whackity [View]