iBATOR is a code generator for iBATIS. iBATOR will introspect one or more database table and will generate iBATIS artifacts that can be used to access the tables.
iBATOR will generate the following artifacts :
- SqlMapXML files (XML files used by iBATIS to map objects and data).
- Java model classes (Match the fields of the table).
- Dao classes (Implements the CRUD operations).
You will still need to hand code SQL and objects for custom queries, or stored procedures.
But what happen to your custom SQL queries when you re-launch iBATOR. Don't worry, any hand coded additions to generated Java classes or SqlMap files will remain undisturbed. The eclipse plugin will merge Java and XML Files.
Let's start,
1. First of all, install the ibator plugin for eclipse using the update site http://ibatis.apache.org/tools/ibator
2. Create a simple java project named ibator. if you have multiple projects, it's a good practice to isolate the ibator generation part. Since I use iBATOR in a pretty big financial project, I have multiple maven module like project-persistence, project-core, project-server, etc... I generate the DAO classes in the persistence project and the domain objects in the core project. If you are, like me, writing some customs plugins for iBATOR, you can put these classes in the ibator project and not polluting your other projects.
3. Right click on your new ibator project and if the plugin is correctly installed, you'll see the entry 'Add iBATOR to the build path'. Click on it.
4. Create the configuration file ibatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ibatorConfiguration
PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN"
"http://ibatis.apache.org/dtd/ibator-config_1_0.dtd">
<ibatorConfiguration>
<ibatorContext
id="ibatorContext"
targetRuntime="Ibatis2Java5"
defaultModelType="flat">
<ibatorPlugin type="org.apache.ibatis.ibator.plugins.SerializablePlugin"/>
<jdbcConnection
driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@host:1521:dbname"
userId="user"
password="password">
</jdbcConnection>
<javaModelGenerator targetPackage="com.yourcompany.vo"
targetProject="yourEclipseProject">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.yourcompany.dao.ibatis.ibatis.maps"
targetProject="yourEclipseProject">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<daoGenerator type="SPRING"
targetPackage="com.yourcompany.dao"
implementationPackage="com.yourcompany.dao.ibatis.impl"
targetProject="yourEclipseProject">
<property name="enableSubPackages" value="true" />
<property name="methodNameCalculator" value="extended" />
</daoGenerator>
<table tableName="MY_TABLE" domainObjectName="yourObject">
</ibatorContext>
</ibatorConfiguration>
I use some special properties in this file. To fully understand the configuration file, see the online documentation.
5. Create a build.xml file at the root of your ibator project.
<project default="runIbator">
<target name="runIbator">
<eclipse.convertPath resourcepath="ibator/resources/ibatorConfig.xml" property="thePath"/>
<ibator.generate configfile="${thePath}" ></ibator.generate>
</target>
</project>
6. Create a lib directory and put your ojdbc jar in it.
7. Create an external tool to run ibator. Go to Run/External tools/External tools configuration... and create a new Ant build configuration. In the main tab, select your build.xml file in the buildfile part. Add your ojdbc jar file in the classpath tab. In the JRE tab, tick 'Run in the same JRE as the workspace'.
8. Run the tool.
Another way would be to use the 'Generate Ibatis artifacts' instead of an external tool when you right click on your project. The problem is, in that case, you don't have any control over the classpath. Since I create custom plugins for iBATOR, I need to add these classes to the classpath.
To conclude, iBATOR is a great tool if you manage to make it work. It can save you a lot of time at the beginning of a project generating all the DAOs and the domain objects.
:)
ReplyDeleteNice POst... got my work done... thanks
ReplyDeletei was struggling with 'Generate iBatis Artifacts' option for long.. the external tool option helped to get things done...
Thanks for this!!
ReplyDeleteHi Julien!
ReplyDeleteThanks a lot for the post, it has been a great time saver!
However, I'm kind of stuck when it comes to custom plugins. I gave it several tries, but couldn't find the proper way to include my custom plugin classes to the classpath.
Could you shade a light on this please?
Thanks,
alexis
Hi Sixel,
ReplyDeleteHere's my real build xml file with the customs plugins part. I didn't include it in this tuto because sometimes I have to launch the task two times to make it works. It's always working the second time though.
<project default="runIbator">
<target name="compileCustomPlugin">
<javac srcdir="src" destdir="target/classes"/>
</target>
<target name="packageCustomPlugin" depends="compileCustomPlugin">
<jar destfile="lib/customIbatorPlugins.jar" basedir="target/classes"></jar>
</target>
<target name="runIbator" depends="packageCustomPlugin">
<eclipse.convertPath resourcepath="spfunds-ibator/resources/ibatorConfig.xml" property="thePath"/>
<eclipse.refreshLocal resource="spfunds-ibator" depth="infinite"/>
<ibator.generate configfile="${thePath}" ></ibator.generate>
</target>
</project>
This build will compile and package your plugins into a jar in a lib directory.
And don't forget to declare your plugins in your ibatorConfig.
it can't work
ReplyDeletethe message error :
[ibator.generate]XML Parser Error on line 1: The XML declaration must end with "?>"
Pls show me how can i fix it
Replace
ReplyDelete<?xml version="1.0" encoding="UTF-8"?/>
by
<?xml version="1.0" encoding="UTF-8"?>
and it should work.
Buildfile: C:\wspace\ibatora\build.xml
ReplyDeletecompileCustomPlugin:
packageCustomPlugin:
[jar] Warning: skipping jar archive C:\wspace\ibatora\lib\customIbatorPlugins.jar because no files were included.
runIbator:
BUILD FAILED
C:\wspace\ibatora\build.xml:15: java.lang.ArithmeticException: / by zero
Total time: 1 second
I think the response of your problem is the warning printed by the packageCustomPlugin task. There's seems to be no classes in your target/classes directory.
ReplyDeleteVery helpful - Thanks...
ReplyDeleteThank you. I looked everywhere to figure out to solve this issue.
ReplyDeleteThanks, that was a big help.
ReplyDeleteThis helped me!! Thank you.
ReplyDeleteHe Respected All,
ReplyDeleteWell I have done with all the things written above... and end up on having following directory struture under the project root
-src
--dao
--impl
--maps
--model
All of the above packages are created using iBator (As I only run the build.xml file).
Now what should I do? what is next? is it all done? what would be the output / how to get the output of this project?
I am not getting a proper essence of why we use iBator?
what would be the output of making this project?
also I read that sqlmap is a AUTOMATIC SQL INJECTION TOOL... so where can i find this AUTOMATIC SQL INJECTION in this project or how to write my own?
Thanks and Regards.
IBator has been made to accelerate the developpement cycle of IT projects using IBatis. Imagine you have 40 SQL tables, you'll have to write a lot of DAOs, SQL Maps and BOs classes. Ibator will generate all these classes for you in a few seconds.
ReplyDeleteOk, but kinly tell me about those .java executable files through which i can use and execute the above mentioned program.
ReplyDeleteI don't use any .java executable files to launch ibator. Like I said in step 7, you have to create an external tool configuration in eclipse (Run/External Tools/External tools configuration...).
ReplyDeleteHi Julien. Thanks for the post. I still have problem where I get "Exception getting JDBC Driver". I've added the ojdbc6.jar file to a lib directory in the project and added it to the run classpath for the ant build but still no dice for some reason. Anything I could be missing?
ReplyDeleteLooks like ibator didn't like ojdbc6.jar. I switched it out with ojdbc14.jar and it worked.
ReplyDeleteGood to know.
ReplyDeleteHi, I have this problem on running build.xml:
ReplyDeleteBuildfile: /home/paolo/EasynetProject/ibator/build.xml
runIbator:
BUILD FAILED
/home/paolo/EasynetProject/ibator/build.xml:5: java.lang.ArithmeticException: / by zero
Total time: 459 milliseconds
Please, how can I fix it?
Hello, I am having the same problem when running Ant:
ReplyDelete"Exception getting JDBC Driver" with mysql-connector-java-5.1.12-bin.jar inside "lib" directory under my project.
What did I do wrong?
Thanx in advance.
Hello again, my previous post about JDBC Driver exception was due misconfiguration in xml file. Working now as intended :)
ReplyDeleteHello, I was having the same problem of "Exception getting JDBC Driver", but I resolved it by putting the ojdbc14.jar in the classpath but now I'm getting this error
ReplyDelete"Excepción de E/S: The Network Adapter could not establish the connection
".
I don't use ant but the "Generate iBAtis Atifacts" option.
Thanks.
Hi All,
ReplyDeleteyou can below code & then you can able to do "Generate iBAtis Atifacts".
add :-
Sandeep
Hi ,
ReplyDeleteclassPathEntry location="C:/workspaceWS1/zzz/lib/sqljdbc4.jar"
in the map config file.
Hi All
ReplyDeleteI am able to generate DAO as well as all the others file.
But i created another class file to test the Example class using criteria class , it gives me exception.
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
EmpExample empExample= new EmpExample();
Criteria criteria= new Criteria();
criteria.andIdEqualTo(new Integer(1));
empExample.createCriteria().andIdEqualTo(1);
/* empExample.createCriteriaInternal().andIdLessThan(4);
empExample.createCriteria().andEmailEqualTo(new String("syadav@cecsoft.com"));
*/
//empExample.createCriteria().andIdNotEqualTo(2).andFirstnameIsNotNull();
//empExample.or(empExample.createCriteria().andEmailIsNotNull());
List contactList = (List)sqlMap.queryForList("dbo_emp.ibatorgenerated_selectByExample", empExample);
for(Emp e : contactList)
{
System.out.println(e.getFirstname());
}
&
the exception is :-
--- Cause: com.ibatis.common.beans.ProbeException: Error getting ordinal list from JavaBean. Cause java.lang.NumberFormatException: For input string: ""
Please help me out from this issue.
wihtout criteria , it is working fine & displaying all the records.
I am just using simple criteria.
Can any body tell me about the cause of this error.
Thanks
Sandy
This comment has been removed by the author.
ReplyDelete"Hello, I am having the same problem when running Ant:
ReplyDelete"Exception getting JDBC Driver" with mysql-connector-java-5.1.12-bin.jar inside "lib" directory under my project.
What did I do wrong? "
To run it you need to add a classPathEntry in your ibatorConfig.xml file like this :
classPathEntry location="E:\TaskManagementApplication\Workspace\IbatorProject\lib\mysql-connector-java-5.1.10-bin.jar"
This entry shud be in ibatorConfiguration tag and not within ibatorContext.
This works !!!!!
Thanks,
Alpesh
hi Dechma, gr8 job. But it would be good if you can explain how to use OR criteria along with AND.
ReplyDeleteI need to generate the example criteria for the following quesry.
SELECT * FROM employee WHERE (email LIKE 'abc%' OR email LIKE '%@abc.com%'
OR email='test@test.com') AND (NAME = 'raj');
any help??
whenever I am trying to build with Ant I am getting the following error.........
ReplyDelete[ibator.generate] XML Parser Error on line 2: The processing instruction target matching "[xX][mM][lL]" is not allowed.
plz help.......
hi all, I have installed the ibator plugin to my eclipse galileo, but could not add it to the build path. Can anyone help please....
ReplyDeleteHi ,
ReplyDeletegetting below error plz help me to resolve
Problem: failed to create task or type eclipse.convertPath