Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
iBatis is a Data persistence framework like Hibernate, JDO and EJB that maps objects
to SQL statements. It is a lightweight framework and persistence API good for persisting
POJOs( Plain Old Java Objects). iBatis is different from Hibernate, JDO since it uses
stored procedures and existing SQL to handle database.
In this tutorial we are going to tell you iBatis configuration for a small application to
run. As it is hard to explain everything in one go… so we have presented this tutorial into
few separate examples. This example is all about to read data from the database and
showing the results on your command prompt. In the second example you will learn to
add more data into the database and after that third example is going to show you how to
delete the data from the records using iBatis.
Now in the first example that is going to show records from the database, we need a
database to run queries, so we are using MySQL 5.0 as database for this example.
Here we are retrieving contact information of few persons. Table structure of "Contact"
table is as given below:
According to this table "Contact" we have to make a POJO class. Our example database
"vin" has a "Contact" table, which have four fields as:
• id
• firstName
• lastName
• email
Contact.java
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
SqlMapConfig.xml
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
Mapping file "Contact.xml" is given as follows which is responsible for our application
to execute SQL queries. Full code of Contact.xml is as follows:
Contact.xml
<sqlMap namespace="Contact">
<!--- Showing all data of table -->
<select id="getAll"
resultClass="Contact">
select * from contact
</select>
</sqlMap>
Now to show data from database we have created a class "IbatisExample", which reads
configuration from SqlMapConfig.xml and then show all data on your console output.
Full source code of IbatisExample.java is as follows:
IbatisExample.java
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
Output:
Contact.java
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
SqlMapConfig.xml
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
For SQL mapping statement we have used <insert> tag. Inside this tag definition we
have defined an "id" which will be used in IbatisInsertion.java file for executing insert
query on database.
<selectKey resultClass="int"
keyProperty="id">
select last_insert_id() as id
</selectKey>
Above lines of code points to next row of the table where the next value is to be inserted.
Contact.xml
<sqlMap namespace="Contact">
<insert id="insert"
parameterClass="Contact">
insert into contact
(firstName,lastName,email)
values (#firstName#,
#lastName#, #email#)
<selectKey resultClass="int"
keyProperty="id">
select last_insert_id() as id
</selectKey>
</insert>
IbatisInsertion.java
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
Output:
As I earlier mentioned.. In this section of iBatis tutorial we are going to delete records
from Contact table, we are using MySQL database "vin".
Contact.java
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
SqlMapConfig.xml
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
In Contact.xml file we are using <delete> tag to delete all records from Contact table.
<delete id="deleteAll"
parameterClass="Contact">
delete from Contact
</delete>
Above lines of code deletes all records from Contact table. Here the defined id
"deleteAll" will be used further in IbatisDeletion class to execute query on the database.
Contact.xml
<sqlMap namespace="Contact">
<!--- Delete data from Contact table -->
<delete id="deleteAll"
parameterClass="Contact">
delete from Contact
</delete>
Above lines of code reads configuration from "SqlMapConfig.xml". Full source code of
IbatisDeletion.java is as follows:
IbatisDeletion.java
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
Output:
Contact.java
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
SqlMapConfig.xml
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
Here in our example we are updating table with an id as specified in our parameter and
therefore for "id" we have assigned "parameterClass" property value to "long".
Contact.xml
<sqlMap namespace="Contact">
<!--- Showing all data of table -->
<select id="getAll"
resultClass="Contact">
select * from contact
</select>
<!--- Update data of Contact table -->
<update id="updateById"
parameterClass="long">
update Contact
set
lastName = 'Raghuwanshi'
where
id=#id#
</update>
</sqlMap>
Now we can execute update command from our java program by the following code as:
sqlMap.update("Contact.updateById",contactId);
IbatisUpdate.java
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
Output:
iBatis ResultMap example
If you are working with iBatis Result Map then you must know that iBatis result maps are
used to provide mapping between the result of database query and object properties of it.
And it is the most common and important feature of iBatis. This section of iBatis tutorial
is just an simple introduction to ResultMap. This will familiarize you that how you
can execute an query with ResultMap. Our Contact.java and SqlMapConfig.xml file
have no change and these are as same as in our previous examples. Code for Contact
POJO is as follows:
Contact.java
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
SqlMapConfig.xml
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
Contact.xml
<sqlMap namespace="Contact">
<!--- Showing data by ID -->
<resultMap id="result"
class="Contact">
<result property="id" column="id"/>
<result property="firstName"
column="firstName"/>
<result property="lastName"
column="lastName"/>
<result property="email"
column="email"/>
</resultMap>
<select id="getById"
resultMap="result">
select * from contact where
id=#id#
</select>
</sqlMap>
To run resultMap example we have to include following line in our java code.
sqlMap.queryForObject("Contact.getById",new Integer(1));
Here we have passed id value "1" for showing all information on that id.
IbatisResultMap.java
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
Output:
As I have mentioned in previous example we are using MySQL database and we are
using a Contact table same as in previous examples. We have created a stored procedure
in "vin" database named as showData() which is showing all contact information of
Contact table. For creating stored procedure first open MySQL and create procedure as
defined below:
DELIMITER $$
DROP PROCEDURE
IF EXISTS
`vin`.`showData`$$
CREATE
PROCEDURE
`vin`.`showData`()
BEGIN
select * from Contact;
END$$
DELIMITER ;
Contact.java
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
SqlMapConfig.xml
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
We have only modified "Contact.xml" and using <procedure> tag for calling stored
procedure.
<procedure id="storedInfo"
resultClass="Contact">
{ call showData() }
</procedure>
Above lines of code is calling stored procedure and results contact lists. Full source code
of Contact.xml is as follows:
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD
SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-
map-2.dtd">
<sqlMap namespace="Contact">
<!--- Calling stored procedure -->
<procedure id="storedInfo"
resultClass="Contact">
{ call showData()}
</procedure>
</sqlMap>
IbatisStoredProcedure.java
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
System.out.println("All Contacts");
List<Contact> contacts = (List<Contact>)
sqlMap.queryForList("Contact.storedInfo",null);
Contact contact = null;
for (Contact c : contacts) {
System.out.print(" " + c.getId());
System.out.print(" " + c.getFirstName());
System.out.print(" " + c.getLastName());
System.out.print(" " + c.getEmail());
contact = c;
System.out.println("");
}
}
}
Output: