21
1 Introduction to iBatis Kunal Umrigar

Introduction to iBatis

Embed Size (px)

Citation preview

Page 1: Introduction to iBatis

1

Introduction to iBatis

Kunal Umrigar

Page 2: Introduction to iBatis

2

Page 3: Introduction to iBatis

3

Problems

Tight integration of SQL code and Java code.

Redundant JDBC code consumes a lot of development time.

Is our DAO really a DAO?

Page 4: Introduction to iBatis

4

Solutions?

Page 5: Introduction to iBatis

5

Hibernate (ORM)

Object-Relational Mapping??

Page 6: Introduction to iBatis

6

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

Page 7: Introduction to iBatis

7

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

Does not fit with legacy or complex Database.

Page 8: Introduction to iBatis

8

An Introduction to Apache iBATIS(i)nternet + A(batis)

SQL Mapping defined...

Page 9: Introduction to iBatis

9

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 ☺

Page 10: Introduction to iBatis

10

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.

Page 11: Introduction to iBatis

11

Page 12: Introduction to iBatis

12

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...

Page 13: Introduction to iBatis

13

The Product Class

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

Page 14: Introduction to iBatis

14

The SQL

<select id=“getProductgetProduct" parameterClass=“int” resultClass="examples.domain.Product">

SELECTPRODUCT_ID as id,NAME,DESCRIPTION,COST,RETAIL,FROM PRODUCTWHERE PRODUCT_ID = #id#

</select>

Page 15: Introduction to iBatis

15

<bean id="productDAO" class="examples.domain.dao.ProductDAO">

<property name="sqlMapClient"><ref bean="sqlMapClientsqlMapClient" />

</property><property name="dataSourcedataSource">

<ref bean="myDataSource" /></property>

</bean>

The Spring DAO Bean

Page 16: Introduction to iBatis

16

public class ProductDAOImpl extends SqlMapClientDaoSupport implements ProductDaoProductDao{ @SuppressWarnings("unchecked") public List<Product> getAllProducts(int id) { List<Product> productList =

getSqlMapClientTemplate().queryForList( "getProduct" , id ); return productList; }

.....

The Product DAO Implementation

Page 17: Introduction to iBatis

17

...“WTF!! Do you mean we need to hand code XML configurations?”

“Yeah!! That's true..”...

Page 18: Introduction to iBatis

18

JDBC

Obtain the db Connection Create the statement Set the input parameters Execute the statement Create the result Collection For each row fetched:

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

IBATIS

Obtain the SqlMap object Prepare complex parameters

(optional)Invoke the query passing the

parametersReturn the result

JDBC-iBatis Comparison

Page 19: Introduction to iBatis

19

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 (populatinglists, complex object models etc.), Transactions, Lazy Loading or Join Mapping (1:1, 1:M, M:N), and caching.

Page 20: Introduction to iBatis

20

       - Full power of real SQL

      - Works with complex, enterprise, ERP or even poorly designed databases

      - Fully supports composite keys and complex relationships

      - Complete stored procedure support

      - JavaBeans support

      - Primitive wrappers (Integer, String, Date etc.)

      - HashMap, List, Collection, and array[]

      - XML text and DOM Support

      - Null value translation

      - Auto-mapping bean properties to columns

    

   - Dynamic SQL using conditional XML tags

      - Caching and dependency management

      - Centralized data source configuration

      - Local and global (JTA) transaction support

      - Small footprint 300k (minimum)

      - Minimal dependencies (only common-logging required)

      - Spring DAO Templates available

iBATIS SQL Mapping Framework

Page 21: Introduction to iBatis

21

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