Before you start
This tutorial is the second in a three-part series that introduces you to the iBATIS Data Mapper and Data Access Objects (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.
This tutorial explains the persistence and hiding of data access/Java Database Connectivity (JDBC)/OR mapping and focuses on the data model and Java object model of the JPetStore sample application in Derby. It then illustrates JPetStore in action using DAO and the Data Mapper by showing (with a modification to JPetStore) how it handles the N+1 select problem. You can expect to:
- Take an in-depth look at the JPetStore sample application.
- Build your database, and look at how all the layers integrate together.
- Examine the full stack of Java business objects -- including the iBATIS DAO framework, the iBATIS Data Mapper framework, and Derby where the application data is persisted
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 environmental 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.
Note that if you don't specify a valid value for this property, the application will likely perform no useful function besides throwing exceptions.

