When working on a software system that is backed by a persistence layer such as a database the developers need to mitigate that connection through some means. Object-relational mapping gives developers the ability to interact with an object instead of sql queries, stored procedures or something else.
The iBATIS framework provides data mapping in a simple and flexible manner and mitigates the transfer of data between your objects and relational database. You can use the full power of SQL without writing a single line of JDBC code. With iBATIS data access objects you can abstract the persistence implementation of your application. Coding to the DAO’s provided by iBATIS will enable your project to be dynamically configured to use different persistence mechanisms through one common interface.
iBator is a code generator for iBATIS. It will introspect a database schema and generate iBATIS artifacts. iBator can be run as a plugin for eclipse, once configured to the database any changes in tables can be quickly enveloped in the iBATIS layer by simply running the iBator generator over the changed database (can also be run as an ant task or a stand alone JAR).
For this project we will be working with Eclipse, iBator, MySQL and Spring. I will be using an example project that I am working on with some friends codenamed ‘chapplet’.
Install iBator as an eclipse plugin
Eclipse is required as a pre-requisite, if you are in ubuntu I wrote a post about this.
- Select “Help>Software Updates…”
- Navigate to the “Available Software” tab
- Press the “Add Site” button
- Enter the following URL into Location:
http://ibatis.apache.org/tools/ibator
- Press OK
- Check the box next to “Apache iBATIS iBator Feature”
- Press the “Install” button
Create a Project and Prepare it for iBATOR
Create a Java project in Eclipse, right click on your project and select “Add iBator Build Path”. This will add the iBATOR jar to the referenced libraries for your project. Now we also need to download and add the MySQL JDBC connector and Spring Jar.
Spring Download Link (I downloaded 2.5.6)
Open the archives and extract the jar files into your project folder. I have created a folder named ‘lib’ in my projects directory and put the jars inside that folder. Now go back to eclipse, right click on your project and select refresh. You should now see ‘lib’ and the two jars inside, right click on each of those jars and add them to the build path.
Now we have everything prepped and we are ready to create the iBATOR configuration file.
iBATOR Configuration
Now we need to download the iBATOR configuration document type definition, save it to a folder inside your project (I used the folder name ‘ibator’).
- Right click on your project and select ‘New>Other…’ expand the XML folder and select an XML file, click next.
- Name the file something like iBATORConfig.xml and place it in the ‘ibator’ folder, click next.
- Select ‘Create XML file from a DTD file’ and click next. Now you can select that iBATOR configuration DTD we downloaded earlier, click next.
- You can see some of the options here, I am not going to fiddle with any of these for the tutorial. Hit finish.
Now you should have ended up with a file that looks like this in the source view:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE ibatorConfiguration PUBLIC “-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN” “ibator/ibator-config_1_0.dtd” >
<ibatorConfiguration>
<classPathEntry location=”locationOfMySQLjar”/>
<ibatorContext id=”idvalue0″>
<jdbcConnection connectionURL=”" driverClass=”"/>
<javaModelGenerator targetPackage=”" targetProject=”"/>
<sqlMapGenerator targetPackage=”" targetProject=”"/>
<table tableName=”"/>
</ibatorContext>
</ibatorConfiguration>
Lets tackle each node individually, the items are nested under in the <ibatorConfiguration></ibatorConfiguration> node. Listed off we have ibatorContext and its sub nodes: jdbcConnection, javaModelGenerator, sqlMapGenerator and tables.
If you would like to read about the options for each property I am setting the iBATOR Documentation is quite comprehensive
ibatorContext
ibatorContext specifies just what it says, an iBATOR context for a specific database. If you wanted to introspect multiple databases with your project then you would have to have multiple ibatorContext nodes, one for each database.
So for this ibatorContext we will specify three values: id, defaultModelType and targetRuntime:
<ibatorContext id=”chappletDev” defaultModelType=”flat” targetRuntime=”Ibatis2Java5″>
jdbcConnection
We are using MySQL for this project and have already added the jdbc connector for MySQL to our build path. Now we have to set the following values for jdbcConnection: connectionURL, driverClass, userId and password:
<jdbcConnection
connectionURL=”jdbc:mysql://devserver.com:3306/chapplet_dev”
driverClass=”com.mysql.jdbc.Driver”
userId=”username”
password=”password”>
</jdbcConnection>
javaModelGenerator
The model generator builds record classes and the query classes from the introspected table. We will be setting the targetPackage and targetProject properties as well as adding the enableSubPackages and trimStrings properties:
<javaModelGenerator
targetPackage=”chapplet.model”
targetProject=”chapplet”>
<property name=”enableSubPackages” value=”true” />
<property name=”trimStrings” value=”true” />
</javaModelGenerator>
sqlMapGenerator
This node will build out the iBATIS formatted sql maps for each introspected table, We will set the targetPackage and targetProject properties as well as add the enableSubPackages property:
<sqlMapGenerator
targetPackage=”chapplet.model.map”
targetProject=”chapplet”>
<property name=”enableSubPackages” value=”true” />
</sqlMapGenerator>
daoGenerator
daoGenerator is a non-essential node for the ibatorContext so you will probably have to add it. It is however the item we are really interested in because it will be creating the data access interfaces that we will be wiring our application to. I wont bother to list the properties, you should be getting the hang of how I am adding them.
<daoGenerator
targetPackage=”chapplet.model.dao”
type=”SPRING”
targetProject=”chapplet”
implementationPackage=”chapplet.model.dao.impl”>
<property name=”enableSubPackages” value=”true” />
<property name=”methodNameCalculator” value=”extended” />
</daoGenerator>
Tables
Now we have to specify all the tables in our schema, for your pleasure I will include a nice diagram of our schema:
We just need to add entries for each table in that schema:
<table tableName=”Buddy” domainObjectName=”Buddy” />
<table tableName=”Chat” domainObjectName=”Chat” />
<table tableName=”Message” domainObjectName=”Message” />
<table tableName=”User” domainObjectName=”User” />
<table tableName=”UserToChat” domainObjectName=”UserToChat” />
Now you are all set with the config, I uploaded my example config so that you could see the whole picture: iBATORConfig
You can right click on the iBATORConfig.xml file and select ‘Generate iBATIS Artifacts’ to run the introspection and code generation.

what to do if there is no ‘Generate iBATIS Artifacts’?
This is most likely a problem with the way you created the configuration file, it is essential that the file has a document type that points to the dtd that was downloaded:
< !DOCTYPE ibatorConfiguration PUBLIC “-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN” “ibator/ibator-config_1_0.dtd” >
notice that it has “ibator/ibator-config_1_0.dtd” in there, you need to make sure that it somehow is pointing to the dtd.
My dtd is in my project root, in a folder named ibator.
It doesn’t work.
When I select the xml file (created based on dtd file), and I press right button, I can not find the ‘Generate iBATIS Artifacts’.
What version of Eclipse are you using?
I have installed Eclipse Galileo J2EE.
I was using Galileo also, did you verify that the doctype was in fact pointing to your dtd?
< !DOCTYPE ibatorConfiguration PUBLIC “-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN” “ibator/ibator-config_1_0.dtd” >
The last part “ibator/ibator-config_1_0.dtd” is a reference to the file. Its context is relative to your project, so for my instance I have a folder named ibator with the file inside it.
Thanks!!
It works now, I think that it was an eclipse error.
But now It works very well.
Thanks for your post and your help.
Thanks for the post,
I would like to know if the generated model and sqlmappings (XML that is) incorporate the relation between your Objects displayed in that diagram. In my case the result maps generated by Ibator only contain the columns of the specified tables, but I would also like the resulMap generated by Ibator to include relations between the Objects. If you got that working I would appreciate an answer on how you did that? Or do you have to implement relations yourself?
Thanks in advance.
Hello,
i’m trying to do exact what you wrote, but it doesn’t work:-(.
When i create the file iBATORConfig.xml from the ibator-config_1_0.dtd, i don’t get the prolog . So i copy your prolog into my iBATORConfig.xml.
When i right klick on iBATORConfig.xml and select “Generate iBATIS Artifacts”, i get a “Generation Failed, Reason: Unexpected error while running Ibator java.lang.NullPointerException”. Please help me…
Hier is my iBATORCOnfig.xml:
It sounds like you literally dont have ibator installed in your eclipse environment. It has been a while since I used this tool and I remember having this issue when I didnt have it properly installed. Eclipse is pretty portable so I would suggest downloading a fresh copy and installing ibator again for another try.
When you cannot find ‘Generate iBATIS Artifacts’, check the iBATORConfig.xml for unusual ["]-Quote characters, i found it the problem when i copy-pasted a whole codes from this blog, then i change that quotes (“), and it helps, the ‘Generate iBATIS Artifacts’ shown. Hope that might be help…
i assume that ‘Generate iBATIS Artifacts’ cannot be show when your iBATORConfig.xml still have errors..