Upload
kunal-umrigar
View
166
Download
5
Tags:
Embed Size (px)
Citation preview
1
Introduction to iBatis
Kunal Umrigar
2
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?
4
Solutions?
5
Hibernate (ORM)
Object-Relational Mapping??
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
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.
8
An Introduction to Apache iBATIS(i)nternet + A(batis)
SQL Mapping defined...
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 ☺
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.
11
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...
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}
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>
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
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
17
...“WTF!! Do you mean we need to hand code XML configurations?”
“Yeah!! That's true..”...
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
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.
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
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