IBatis
IBatis maps the Java Objects to the SQL Query’s results. Hibernate, maps Java Objects to DB tables (Object-Relational Mapping). Hibernate automatically generates all the SQL for you. In IBatis you can directly write queried in SQL, thus if you are a good SQL programmer, you would find learning Ibatis very easy. Thus Ibatis allows you to do anything what could be done through SQL. Thus you can virtually know what the affect of a query would be.
· Hibernate is excellent for CRUD queries (Create, Read, Update, Delete) queries, and in applications where the object model is already ready.
· IBatis is much simpler to learn compared to Hibernate .
· On the other hand, Since you need to write all the queries by yourself, It has a much steeper development time and maintainability is comparatively difficult in Ibatis.
· Also if there is a change in object – model, one needs to go through all the queries and correct them, which isn’t much of a problem in hibernate.
· Hibernate does not support stored procedures. Ibatis does.
· IBatis is faster in terms of query processing time
· IBatis is better for batch inserts queries.
· IBatis is simpler.
· IBatis supports Dynamic Query which Hibernates doesn’t.
· Hibernate is close to Object Oriented development. Ibatis is close to relational DB.
Steps :
1) Creata a databas and a table
2) Create a package com.Employee
3) SQL mapper – Employee.xml
4) SQL configuration file : SQLMapConfig.xml
5) Create Java Application
Step 1: Create a table
use employeedb;
Query to create table :
create table employee (
employeeID integer(10) primray key auto_increment,
firstName varchar(255) not null,
lastName varchar(255) not null);
Insert some records into a table:
insert into employee(firstName,lastName,email) values
("sandeep","gupta","[email protected]");
insert into employee(firstName,lastName,email) values
("ashim","devnath","[email protected]");
insert into employee(firstName,lastName,email) values
("abhishek","kumar","[email protected]");
Step 2: Create a package and a java object .
package com.Employee
import java.io.Serializable;
public class Employee implements Serializable {
private int employeeID;
private String firstName;
private String lastName;
private String email;
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
…..
}
Similarly have a getter and setter for other attributes as well .
Step 3: Create struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC “-//ibatis.apache.org//DTD SQL Map Config 2.0//EN”
“http://ibatis.apache.org/dtd/sql-map-config-2.dtd”>
<sqlMapConfig>
<transactionManager ”JDBC”>
<dataSource ”SIMPLE”>
<property name=”JDBC.Driver” value=”com.mysql.jdbc.Driver”/>
<property name=”JDBC.ConnectionURL”
value=”jdbc:mysql://localhost:3306/employeedb”/>
<property name=”JDBC.Username” value=”root”/>
<property name=”JDBC.Password” value=”"/>
</dataSource>
</transactionManager>
<sqlMap resource=”com/Employee.xml”/>
</sqlMapConfig>
Steps 4 : Create Employee.xml
<?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>
<select id=“getEmployee” parameter“int”
result“com.Contact”>
SELECT employeeID as employeeID,
firstName as firstName,
lastName as lastName,
email as email
from Employee
where employeeID=#value#
</select>
<select id=“getEmployees” result“com.Employee”>
SELECT employeeID as employeeID,
firstName as firstName,
lastName as lastName,
email as email
from Employee
</select>
</sqlMap>
Step 5 : Create a standalone java class to run the application .
import java.io.Reader;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.model.Employee;
public class QueryEmployee {
public static void main(String[] args) {
try {
String resource = “SqlMapConfig.xml”;
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
Integer employeePrimaryKey = new Integer(1);
Employee employee =
(Employee)sqlMap.queryForObject(”getEmployee”,
employeePrimaryKey);
System.out.println(employee.getFirstName()
+ “\t” + employee.getLastName());
}catch(Exception e) {
e.printStackTrace();
}
}
}
SQLMapConfig.xml Basics This is where you need to provide all configurations for iBatis. Create an XML file with name SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "
http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<transactionManager >
<dataSource >
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="JDBC.Username" value="root"/> <property name="JDBC.Password"
value=""/>
</dataSource>
</transactionManager>
<sqlMap resource="com/Employee.xml"/> </sqlMapConfig>
Lets understand the above code :
<transactionManager ”JDBC”>
type can be JDBC, JTA,External
JDBC Alias for com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTrasactionConfig
JTA alias for com.ibatis.sqlmap.engine.transaction.jta.JtaTransactionConfig
External alias for com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
<dataSource ”SIMPLE”>
datasouce can be SIMPLE, DBCP, JNDI
SIMPLE is an alias for com.ibatis.sqlmap.engine.datasource.
SimpleDataSourceFactory DBCP is an alias for com.ibatis.sqlmap.engine.datasource.
DbcpDataSourceFactory JNDI is an alias for com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory.
While using JNDI :
Use the following code
<dataSource ”JNDI”>
<transactionManager >
<property name = "UserTransaction" value ="java:/emp/EmlpoyeeTransaction" />
<datasource type ="JNDI" > <property name ="DataSource" value ="java:/comp
/Employee" />
<datasource>
</transactionManager>
* <property name = "UserTransaction" is optional
DataSource properties :
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
provide the driverName
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/testdb"/>
provide the connection URL
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value=""/>
provide the username and password
Optional properties
<property name="JDBC.AutoCommit" value="true"/>
<property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="100000"/>
<property name="Pool.MaximumTimeToWait" value="400"/>
<property name="Pool.PingQuery" value="select 1 from Enployee"/>
<property name="Pool.PingEnabled" value="false"/>
<sqlMap resource="com/Employee.xml"/>
Used to add the xml where the sql queries have been written in another XML file . Should be present in the class path. Alternatively to load files from a URL <sqlMap url="file://c:/Employee/Employee.xml"/> Related Articles:
· Hibernate is excellent for CRUD queries (Create, Read, Update, Delete) queries, and in applications where the object model is already ready.
· IBatis is much simpler to learn compared to Hibernate .
· On the other hand, Since you need to write all the queries by yourself, It has a much steeper development time and maintainability is comparatively difficult in Ibatis.
· Also if there is a change in object – model, one needs to go through all the queries and correct them, which isn’t much of a problem in hibernate.
· Hibernate does not support stored procedures. Ibatis does.
· IBatis is faster in terms of query processing time
· IBatis is better for batch inserts queries.
· IBatis is simpler.
· IBatis supports Dynamic Query which Hibernates doesn’t.
· Hibernate is close to Object Oriented development. Ibatis is close to relational DB.
Steps :
1) Creata a databas and a table
2) Create a package com.Employee
3) SQL mapper – Employee.xml
4) SQL configuration file : SQLMapConfig.xml
5) Create Java Application
Step 1: Create a table
use employeedb;
Query to create table :
create table employee (
employeeID integer(10) primray key auto_increment,
firstName varchar(255) not null,
lastName varchar(255) not null);
Insert some records into a table:
insert into employee(firstName,lastName,email) values
("sandeep","gupta","[email protected]");
insert into employee(firstName,lastName,email) values
("ashim","devnath","[email protected]");
insert into employee(firstName,lastName,email) values
("abhishek","kumar","[email protected]");
Step 2: Create a package and a java object .
package com.Employee
import java.io.Serializable;
public class Employee implements Serializable {
private int employeeID;
private String firstName;
private String lastName;
private String email;
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
…..
}
Similarly have a getter and setter for other attributes as well .
Step 3: Create struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC “-//ibatis.apache.org//DTD SQL Map Config 2.0//EN”
“http://ibatis.apache.org/dtd/sql-map-config-2.dtd”>
<sqlMapConfig>
<transactionManager ”JDBC”>
<dataSource ”SIMPLE”>
<property name=”JDBC.Driver” value=”com.mysql.jdbc.Driver”/>
<property name=”JDBC.ConnectionURL”
value=”jdbc:mysql://localhost:3306/employeedb”/>
<property name=”JDBC.Username” value=”root”/>
<property name=”JDBC.Password” value=”"/>
</dataSource>
</transactionManager>
<sqlMap resource=”com/Employee.xml”/>
</sqlMapConfig>
Steps 4 : Create Employee.xml
<?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>
<select id=“getEmployee” parameter“int”
result“com.Contact”>
SELECT employeeID as employeeID,
firstName as firstName,
lastName as lastName,
email as email
from Employee
where employeeID=#value#
</select>
<select id=“getEmployees” result“com.Employee”>
SELECT employeeID as employeeID,
firstName as firstName,
lastName as lastName,
email as email
from Employee
</select>
</sqlMap>
Step 5 : Create a standalone java class to run the application .
import java.io.Reader;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.model.Employee;
public class QueryEmployee {
public static void main(String[] args) {
try {
String resource = “SqlMapConfig.xml”;
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
Integer employeePrimaryKey = new Integer(1);
Employee employee =
(Employee)sqlMap.queryForObject(”getEmployee”,
employeePrimaryKey);
System.out.println(employee.getFirstName()
+ “\t” + employee.getLastName());
}catch(Exception e) {
e.printStackTrace();
}
}
}
SQLMapConfig.xml Basics This is where you need to provide all configurations for iBatis. Create an XML file with name SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "
http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<transactionManager >
<dataSource >
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="JDBC.Username" value="root"/> <property name="JDBC.Password"
value=""/>
</dataSource>
</transactionManager>
<sqlMap resource="com/Employee.xml"/> </sqlMapConfig>
Lets understand the above code :
<transactionManager ”JDBC”>
type can be JDBC, JTA,External
JDBC Alias for com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTrasactionConfig
JTA alias for com.ibatis.sqlmap.engine.transaction.jta.JtaTransactionConfig
External alias for com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
<dataSource ”SIMPLE”>
datasouce can be SIMPLE, DBCP, JNDI
SIMPLE is an alias for com.ibatis.sqlmap.engine.datasource.
SimpleDataSourceFactory DBCP is an alias for com.ibatis.sqlmap.engine.datasource.
DbcpDataSourceFactory JNDI is an alias for com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory.
While using JNDI :
Use the following code
<dataSource ”JNDI”>
<transactionManager >
<property name = "UserTransaction" value ="java:/emp/EmlpoyeeTransaction" />
<datasource type ="JNDI" > <property name ="DataSource" value ="java:/comp
/Employee" />
<datasource>
</transactionManager>
* <property name = "UserTransaction" is optional
DataSource properties :
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
provide the driverName
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/testdb"/>
provide the connection URL
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value=""/>
provide the username and password
Optional properties
<property name="JDBC.AutoCommit" value="true"/>
<property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="100000"/>
<property name="Pool.MaximumTimeToWait" value="400"/>
<property name="Pool.PingQuery" value="select 1 from Enployee"/>
<property name="Pool.PingEnabled" value="false"/>
<sqlMap resource="com/Employee.xml"/>
Used to add the xml where the sql queries have been written in another XML file . Should be present in the class path. Alternatively to load files from a URL <sqlMap url="file://c:/Employee/Employee.xml"/> Related Articles: