Introduction To Ibatis
Introduction To Ibatis
Kunal Umrigar
1
2
Problems
3
Solutions?
4
Hibernate (ORM)
Object-Relational Mapping
??
5
Hibernate/Object-Relational Mapping
Tools/techniques to store and retrieve objects from a
database
Transactional Capability
6
Hibernate pros and cons
Pros:
No SQL coding required
Database vendor independent
Provides caching mechanisms
Cons:
Requires learning of quite a lot of stuff
Limits queries to HQL syntax
Debugging and performance tuning is sometimes quite
complected
7
Does not fit with legacy or complex Database.
An Introduction to Apache iBATIS
(i)nternet + A(batis)
8
About iBATIS
9
iBatis Components
iBATIS usage is divided into two main components:
SQLMaps
Permits to read/save Java objects into relational
DBMS without using JDBC and without mixing
Java and SQL code
NOT generated SQL (although that’s possible)
For example...
12
The Product Class
13
The SQL
<select id=“getProduct"
getProduct
parameterClass=“int”
resultClass="examples.domain.Product">
SELECT
PRODUCT_ID as id,
NAME,
DESCRIPTION,
COST,
RETAIL,
FROM PRODUCT
WHERE PRODUCT_ID = #id#
</select>
14
The Spring DAO Bean
<bean id="productDAO"
class="examples.domain.dao.ProductDAO">
<property name="sqlMapClient">
<ref bean="sqlMapClient"
sqlMapClient />
</property>
<property name="dataSource">
dataSource
<ref bean="myDataSource" />
</property>
</bean>
15
The Product DAO Implementation
@SuppressWarnings("unchecked")
public List<Product> getAllProducts(int id)
{
List<Product> productList =
getSqlMapClientTemplate().queryForList(
"getProduct" , id );
return productList;
}
.....
16
...
“WTF!! Do you mean we need to hand
code XML configurations?”
17
JDBC-iBatis Comparison
JDBC IBATIS
Create an object
Set object’s properties using
row’s colums
Add the object to the
Collection
Release resources
Close the Connection
Return the Collection
18
5 Reasons to use iBatis
1. Works with any database that has a JDBC driver
(can re-use existing MySQL queries ) & You already
know SQL, why waste timelearning something else?
- Works with complex, enterprise, ERP or - Dynamic SQL using conditional XML
even poorly designed databases tags
- Fully supports composite keys and - Caching and dependency management
complex relationships
- Centralized data source configuration
- Complete stored procedure support
- Local and global (JTA) transaction
- JavaBeans support support
- Primitive wrappers (Integer, String, - Small footprint 300k (minimum)
Date etc.)
- Minimal dependencies (only common-
- HashMap, List, Collection, and array[] logging required)
- XML text and DOM Support - Spring DAO Templates available
Books:
iBATIS In Action - Clinton Begin (Manning)
http://en.wikipedia.org/wiki/IBATIS
http://ibatis.apache.org/onlinehelp.html
http://www.javabeat.net/articles/52-spring-ibatis-integration-1.html
http://www.learntechnology.net/content/ibatis/spring_ibatis.jsp
Using-iBatis-SQL-Maps-for-Java-Data-Access
21