Tutorial On Ibatis
Tutorial On Ibatis
1. First of all, install the ibator plugin for eclipse using the update site http://ibatis.apache.org/tools/ibator 2. Step 2. Requirements and Project Configuration:
Create a java project MyIbatisTestPr on your eclipse IDE. To setup ibator to build path, right click on project choose Add Ibator to Build Path. This will automatically add the IBATOR_JAR to your referenced libraries. You must manually add all the other library shown in figure below.
Step 3. Now create bookstore database on MySQL. Run the following SQL.
CREATE database IF NOT EXISTS `bookstore`; -- Tables CREATE TABLE IF NOT EXISTS `author` ( `authorid` varchar(10) NOT NULL, `authname` varchar(40) NOT NULL, `authaddress` varchar(40) DEFAULT NULL, PRIMARY KEY (`authorid`) ); CREATE TABLE IF NOT EXISTS `book` ( `bookid` varchar(10) NOT NULL, `bookname` varchar(50) DEFAULT NULL, `authorid` varchar(10) NOT NULL, PRIMARY KEY (`bookid`) ); -- Values INSERT INTO `author` (`authorid`, `authname`, `authaddress`) VALUES ('A001', 'Ram', 'KTM'), ('A002', 'Shyam', 'NGT'); ('A003', 'Hari', 'BRT'); ('A004', 'David', 'UK'); ('A005', 'Micheal', 'USA'); ('A006', 'John', 'AUS'); INSERT INTO `book` (`bookid`, `bookname`, `authorid`) VALUES ('B001', 'Rams'' New Book', 'A001'), ('B002', 'Rams'' second book', 'A001'), ('B003', 'Hariyo dada mathi', 'A002'), ('B004', 'Halo jotne sathi', 'A002');
Step 4: Settings for ibator Now Create ibatorConfig.xml file on your project folder. and define appropriate target packages for javaModelGenerator, sqlMapGenerator and daoGenerators.
<ibatorConfiguration> <classPathEntry location="E:\LAB\ALL JARs\mysql-connector-java-5.1.7-bin.jar/> <ibatorContext id="ibatorContext" targetRuntime="Ibatis2Java5" defaultModelType="flat"> <ibatorPlugin type="org.apache.ibatis.ibator.plugins.SerializablePlugin/> <!-- Setting database connection --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bookstore" userId="user"
password=""> </jdbcConnection> <!-- Settings for generating persistence models --> <javaModelGenerator targetPackage="com.bookStore.model" targetProject="MyIbatisTestPr"> <property name="enableSubPackages" value="true/> <property name="trimStrings" value="true/> </javaModelGenerator> <!-- For Auto-generated SqlMaps --> <sqlMapGenerator targetPackage="com.bookStore.dbMaps" targetProject="MyIbatisTestPr"> <property name="enableSubPackages" value="true/> </sqlMapGenerator> <!-- For Auto-generated DAOs --> <daoGenerator type="SPRING" targetPackage="com.bookStore.service" implementationPackage="com.bookStore.service.impl" targetProject="MyIbatisTestPr"> <property name="enableSubPackages" value="true/> <property name="methodNameCalculator" value="extended/> </daoGenerator> <!-- mapping for tables --> <table tableName="book" domainObjectName="Book/> <table tableName="author" domainObjectName="Author/> </ibatorContext> </ibatorConfiguration>
Step 5: Now ready for using IBator Just right click ibatorConfig.xml on package explorer. And choose Generate iBATIS Artifacts.
Running Ibator
If your MySql server is running and all settings is correct, ibator will generate the models, DAO and sqlmap into corresponding directory. The generated Files
<!-- SqlMap Client Configuration, to read generated SqlMaps, as well as Custom Sqls in separate xml file --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="sqlMapConfig.xml/> <property name="dataSource" ref="dataSource/> </bean> <bean id="bookService" class="com.bookStore.service.impl.BookDAOImpl"><property name="sqlMapClient" ref="sqlMapClient/></bean> <bean id="authorService" class="com.bookStore.service.impl.AuthorDAOImpl"> <property name="sqlMapClient" ref="sqlMapClient/> </bean>
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver/> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/bookstore/> <property name="JDBC.Username" value="user/> <property name="JDBC.Password" value="/> </dataSource> </transactionManager> <!-- add generated sql maps --> <sqlMap resource="com/bookStore/dbMaps/author_SqlMap.xml/> <sqlMap resource="com/bookStore/dbMaps/book_SqlMap.xml/> <!-- <sqlMap resource="customSqlMap.xml/> --> </sqlMapConfig>
3.Test Application
Before testing the application, be sure that your package structure is as follows:
Adding Book:
3.d)Reading Data By Dynamic Query A.)First Create customSqlMap.xml file. The select query getBooks in namespace BookStoreSqlMap is a dynamic query, it returns list of books depending upon the combinations of three dynamic criteria.
<sqlMap namespace="BookStoreSqlMap"> <!-- custom sqls --> <select id="getBooks" parameterClass="com.bookStore.model.Book" resultClass="com.bookStore.model.Book"> SELECT * FROM book WHERE 1=1 <dynamic> <isNotNull prepend="AND" property="bookid"> bookid=#bookid# </isNotNull> <isNotNull prepend="AND" property="bookname"> bookname=#bookname# </isNotNull> <isNotNull prepend="AND" property="authorid">
</isNotNull>
Download Eclipse Project with DB schema from here. References: http://ibatis.apache.org/docs/tools/ibator/index.html http://ibatis.apache.org/ibator.html http://ibatis.apache.org/docs/tools/abator/