| Sign In/My Account | View Cart |
iBatis DAO
Pages: 1, 2, 3, 4, 5
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:
transactionManager in
your DAOMap.xml file.
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.
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.
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.
Showing messages 1 through 19 of 19.
Its good article and informative. I working in iBATIS1.8 with STRUTS framework.
Can you help me in
1) finding the sample code for iBATIS 2.x.
2) Sample code for Multiple data sources, how can I setup datasource in iBATIS for multiple datasources.
3) how can I use ref-cursor for iBATIS.
Thanks
Venkat