Spring + iBATIS Web Example
To demonstrate the integration between Spring and iBATIS, the example uses a simple Web-based contact database. This application is very barebones and incorporates several shortcuts to keep the focus on showing a simple, yet non-trivial integration between these two technologies.
Demonstration Data Model
The data model is very straightforward, consisting of three tables:
- PersonEach contact within the application has a first name, last name, and unique ID that are maintained in this table.
- AddressContacts can have one or more physical addresses registered in this table and associated with the person's unique ID.
- EmailContacts can have one or more email addresses registered in this table and associated with the person's unique ID.
Demonstration Use Cases
The Spring/iBATIS contact database supports the following use cases:
- Query physical addresses by matching contact first or last names in whole or in part
- Query digital addresses by matching contact first or last names in whole or in part
- Add new people to the database
- Add new addresses for existing people in the database
In order to keep the demonstration simple and maintain brevity within the source code, the application leaves out the standard capabilities of editing/updating, deleting, and custom reporting on data.
Running the Application
In order to run the application, you need a JDK 1.4+ environment and a J2EE-compliant Web server (Tomcat, Resin, Jetty, etc.) that supports JDK 1.4+. All of the other libraries you need (Spring, iBATIS, JSTL, HSQLDB) are available in the downloadable source code at the bottom of the page. Take the following steps to use the source code:
- Unzip the downloadable archive somewhere on your machine.
- Take the WAR file (demo.war) from the root of that archive and deploy it to your J2EE server (for Tomcat, simply drop the WAR file into the webapps directory).
- Launch the database server by running the startDB.bat file located in the db directory.
- Start your J2EE server and verify that the server starts without errors.
- Open a Web browser and browse to the demo application running on your server (probably http://localhost:8080/demo or http://localhost/demo). Figure 3 shows the demo on Mozilla Firefox.
|Figure 3. Demo Application on Mozilla Firefox|
- Have fun!
Don't Overlook iBATIS
The Spring Framework provides an excellent backbone for loosely coupling components and even other frameworks together to provide technologists with maximum flexibility and reuse. Across the myriad of persistence layer options available in the marketplace today, many software engineers have overlooked iBATIS. After reading this article and exploring the demonstration application, you should now have a better appreciation for what iBATIS has to offer and when you would want to use it in your work. With its low barriers to entry, transparent utilization of SQL, cleanly divided separation of responsibilities, and elegant integration with Spring, the strengths of iBATIS within today's computing environment are self-evident.