Using iBator to auto-generate ibatis db related artifacts

Posted by Cliff Fenwick Gibb Cliff Fenwick Gibb   star
Options

Assuming a basic understanding of iBatis, and why it is such a great choice as a database framework, I will skip ahead to the topic of configuring the classes and sql map files that iBatis requires.

As simple as it is to configure and use iBatis, as your application grows and the data model expands, you will require a growing number of sql map files, and supporting java classes (the model class, and the dao classes).

This is where iBator comes to the rescue. This is an application (that can integrate with eclipse) that generates all of these iBatis assets for you!

Step 1: Install iBator


Click here to go to the iBator website, then scroll down to the section titled "Automatic Eclipse Install". Follow these instructions.

Step 2: Create the iBator config file


Within eclipse, create a file named "abatorConfig.xml" (yes, the product used to be call abator, so there are some legacy references to this name).
This config file is used to define how iBator will connect to your database, where to generate your sql maps, model classes, and dao classes, and finally what tables are to be included from your database.

I have uploaded a portion of my config file for your reference, and also to point out a few special cases: abatorConfig.xml

Special case #1: Defining CLOBS
As you can see in the config file, the 'business' table has a few CLOBs, but iBator doesn't recognize this field type, so you must override the column, and specify it as VARCHAR.

Special case #2: Changing the model object name
The normal process for iBator is to create model classes with the name matching the table name. For example, the 'User' table will be converted into 'User' a object.
But in my case, I have a table named "geolitecitylocation", and I wanted the object to simply be called "Location". The sample config file demonstrates how to do this.

Step 3: Generate the artifacts!


Right-mouse-click over the abatorConfig.xml in eclipse, and select "Generate iBatis Artifacts".
Now you will see all of the model, dao, and sql map xml files created in the designated package structure.

Step 4: Update the SqlMapConfig.xml


Each of the sql map xml files must be referenced in the SqlMapConfig file.
I have uploaded a portion of my config file for your reference:
SqlMapConfig.xml

Now, if you update a table by adding a new column, you simply need to re-execute the "Generate iBatis Artifacts" process, and the classes and sql maps will be updated automatically!
There are a couple of exceptions to this rule: If you add a new table, you must also update the SqlMapConfig file (to include the reference to the new sql map xml file).

Also, I have had to make some manual changes to the DAO Impl java files so that Spring can manage the DAOs as Spring managed beans.

Manual changes to the DAO Impl java files


In order to allow the DAO to be used as Spring managed beans, you must modify the DAOImpl classes to extend SqlMapClientDaoSupport.

For example:
public class BusinessDAOImpl extends SqlMapClientDaoSupport implements BusinessDAO { ...
}

More to come in following posts of how to configure Spring to use the DAO as Spring managed beans!

Loading...