Sei sulla pagina 1di 21

Introduction to iBatis

Kunal Umrigar

1
2
Problems

 Tight integration of SQL code and Java code.

 Redundant JDBC code consumes a lot of development time.

 Is our DAO really a DAO?

3
Solutions?

4
Hibernate (ORM)

Object-Relational Mapping
??

5
Hibernate/Object-Relational Mapping
Tools/techniques to store and retrieve objects from a
database

From the code perspective it behaves like a virtual


object database

 A complete ORM solution provide:


 Basic CRUD functionality

 An Object-Oriented Query Facility

 Mapping Metadata support

 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)

SQL Mapping defined...

8
About iBATIS

 Open source framework that reduces the complexity to


read/save Java objects from a database (persistence)

 Decouples Java code from SQL

 Avoids the necessity to use JDBC

 It is NOT an object-relational mapper (such as


Hibernate)

 Simplifies the developer’s life ☺

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

 DAO <Spring framework DAO>


“Is an abstraction layer that hides the details of
your persistence solution and provides a common
API to the rest of your application”
Note: previous versions of iBatis had separate DAO component which is deprecated in the
latest version iBatis 3.
10
11
Sql Mapping

SQL Mapping Maps objects to SQL statements


 NOT Classes to Tables

Fully functional SQL via named statements



NOT generated SQL (although that’s possible)

For example...

12
The Product Class

public class Product


{
private int id;
private String name;
private String description;
private BigDecimal cost;
private BigDecimal retail;
// ...getters/setters implied
}

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

public class ProductDAOImpl extends


SqlMapClientDaoSupport implements
ProductDao
{

@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?”

“Yeah!! That's true..”


...

17
JDBC-iBatis Comparison
JDBC IBATIS

 Obtain the db Connection Obtain the SqlMap object


 Create the statement Prepare complex parameters

 Set the input parameters (optional)


 Execute the statement Invoke the query passing the

 Create the result Collection parameters


 For each row fetched: Return the result

 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?

2. Supports Map, Collection, List and Primitive


Wrappers (Integer, String etc.)

3. Easy integration with Spring DAO.

4. Works well with legacy DB and complex relational


mappings between DB tables.

5. Supports complex object mappings (populating


lists, complex object models etc.), Transactions, Lazy
Loading or Join Mapping (1:1, 1:M, M:N), and caching. 19
iBATIS SQL Mapping Framework

       - Full power of real SQL

      - 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

      - Null value translation

      - Auto-mapping bean properties to 20


columns

    
Books:


iBATIS In Action - Clinton Begin (Manning)

Links for reference:

 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

Potrebbero piacerti anche