Before you start
This tutorial is the third in a three-part series that introduces you to the iBATIS Data Mapper and DAO frameworks and shows you how to use iBATIS with Apache Derby.
Part 1 discusses the concepts underlying iBATIS, including descriptions of the Data Mapper and DAO frameworks, and outlines the semantics and terminology that iBATIS uses. You look at some basic examples of how to configure Apache Derby with iBATIS and perform simple OR mapping using Data Mapper.
Part 2 covers the DAO and the Data Mapper frameworks in more depth, using the JPetStore example application to illustrate important iBATIS concepts. Using the DAO with the Data Mapper provides you with a clean and consistent way to access your application's underlying data structure.
Part 3 continues the use of the two frameworks with JPetStore and covers transaction handling, data caching, and creating dynamic SQL statements. It also takes a brief look at, and provides an example of, how to use the DAO framework without the Data Mapper framework to integrate it as a stand-alone framework within an application.
At this point you should have a good idea of how the DAO and Data Mapper frameworks work within iBATIS and how iBATIS avoids the N+1 select problem. In this tutorial you look at how iBATIS can help you with three advanced features that are important when you use any database with a Java application:
- Transaction handling: Transactions are essential for data integrity and are available to both the DAO and Data Mapper frameworks. In this tutorial's example, the DAO framework delegates transaction management to the underlying Data Mapper framework.
- Data caching: Data caching enables large database applications to scale under heavy load. This feature pertains to the iBATIS Data Mapper framework only, not to the DOA. (The DAO provides access to underlying data and isn't responsible for where the data comes from or how it works.)
- Creating dynamic SQL statements: Dynamic SQL lets you generate SQL statements based on certain criteria without needing to write complex and extensive Java code. Like data caching, this feature pertains only to the Data Mapper framework.
You use Apache Ant to execute most of the examples from the command line, which provides a clear view of what's happening in the database. You also continue to use the modified version of the JPetStore application from Part 2 to build up any additional examples that are needed.
Finally, you learn how to use the DAO framework separately from the Data Mapper framework. The ability to use the two frameworks together or individually illustrates the power and flexibility of iBATIS.
This tutorial assumes you're comfortable working with basic SQL statements, that you understand basic XML semantics, and that you can read Java code. You need to know how to run Apache Ant to follow the tutorial's examples and create the database.
To run the example code in this tutorial, perform the following steps:
- Download and install the following applications: You don't need to download iBATIS separately, because the necessary .jar files are included with the source code for this tutorial, which you download in step 3.
- Make sure that the environment variables outlined in Table 1 are defined in your shell.
Table 1. Setting the environmental variables Variable name Required setting DERBY_HOMESet to the root folder of your Derby installation. ANT_HOMESet to the root folder of your Ant installation. JAVA_HOMESet to the root folder of your Java installation. PATHEnsure that ANT_HOME/bin is in your path. - Extract the supplied .zip file (see the Download section) to your preferred location. (This is the project root.) The project is laid out in Listing 1:
Listing 1. Project layout/iBATIS_JPetStore-4.0.5/ /build/ Ant build.xml and build output /ddl/ DDL to create our database and data load SQL /devlib/ JARs needed for app compilation /doc/ licensing and stuff /lib/ JARs needed for application execution /src/ Java source code and iBATIS config files /web/ JSP, Struts, and webapp stuff
- Modify the /src/properties/database.properties file to specify where you would like Derby to create your database. I've set the
urlproperty in the database.properties file to bejdbc:derby:c:/temp/ibatis. See the database.properties file, shown in Listing 2, and change the path to include something appropriate for your file system.
Listing 2. The database.properties file#################################### # Database Connectivity Properties #################################### driver=org.apache.derby.jdbc.EmbeddedDriver url=jdbc:derby:c:/temp/ibatis username= password=
Make sure that the value you specify is an existing empty directory. You can leave the username and password blank, as they are. If you're on a UNIX® or Linux® system, your path looks something like /tmp/ibatis, but you can create this directory wherever you want.

