Before you start
This tutorial is the first 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 object-relational (OR) mapping using Data Mapper.
Part 2 focuses on the DAO framework, from the application layer down to the data-access and Derby database layers. You use the DAO framework with the Data Mapper framework through the JPetStore application. Using the DAO with the Data Mapper provides you with a clean and consistent way of accessing your application's underlying data structure.
Part 3 uses the Data Mapper to cover 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.
As a developer, you've likely had the frequent responsibility of mapping your Java objects to relational database tables for persistence. Persistence is the ability to read objects from, write objects to, and delete objects from permanent storage -- a topic discussed ad infinitum in the Java community. This is where iBATIS comes in. It's a simple and powerful framework that does a great job of OR mapping, letting you solve business problems instead of mucking around with boilerplate code.
iBATIS consists of two frameworks, one of which -- the Data Mapper -- pertains specifically to OR mapping. The Data Mapper is this tutorial's main focus. You'll use the versatile Apache Derby as an embedded database for your persistence layer in the tutorial's example exercises.
This tutorial assumes that you're comfortable working with basic SQL statements, that you understand basic XML semantics, and that you don't have any trouble reading through 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/ /lib/ ' iBATIS JAR files to be included in the classpath /sql/ ' file containing DDL to create your database /src/ ' Java source code and iBATIS XML configs /build.xml ' Ant build file
- 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.



