Sunday 21 August 2011

iBatis

iBatis

The iBatis framework is a lightweight data mapping framework and persistence API that can be used to quickly leverage a legacy database schema to generate a database persistence layer for your Java application. A set of XML encoded SQL Map files–one for each database table–holds SQL templates that are executed as prepared statements and map the expected results to Java domain classes. From application code, a layer of iBatis Data Access Objects (DAO) acts as the API that executes the SQL Map templates and assigns the results to the corresponding Java domain classes. Therefore, the architectural stack looks like this:


Application Layers

Usage

For example, assume there is a database table PRODUCT (PROD_ID INTEGER, PROD_DESC VARCHAR(64)) and a Java class com.example.Product (id: int, description: String). To read the product record having the key PROD_ID into a new Product POJO, the following mapping is added into an iBATIS XML mapping file:
<select id="getProduct" parameterClass="java.lang.Long" resultClass="com.example.Product">
select PROD_ID as id,
PROD_DESC as description
from PRODUCT
where PROD_ID = #value#
</select>
A new Java Product object can then be retrieved from the database for product number 123 as follows:
Product resultProduct = (Product) sqlMapClient.queryForObject("getProduct", 123);
In the mapping file example, #value# refers to the long integer value passed into the query. If the parameter is a Java object, then values from properties on that object can be inserted into the query using a similar # notation. For example, if the parameter class is a com.example.Product which has a property called id, then #value# can be replaced with #id#. The sqlMapClient object is an instance of class com.ibatis.sqlmap.client.SqlMapClient.

Availability

The founder of iBATIS has publicly stated his dismay with Java 5, but has continued to release new versions of iBATIS for Java. Versions 2.3.1 and 2.3.2 came out in April 2008, and 2.3.3 in July. Work is also underway on release 3.0.0.
The framework is currently available in Java, .NET, and Ruby (RBatis) versions. The jBati project is a JavaScript ORM inspired by iBATIS.
The Apache iBator tool is closely related: it connects to your database and uses SQL introspection to generate iBATIS mapping files and Java classes.

What is iBatis ?

  • A JDBC Framework
  • Developers write SQL, iBATIS executes it using JDBC.
  • No more try/catch/finally/try/catch.
  • An SQL Mapper
  • Automatically maps object properties to prepared statement parameters.
  • Automatically maps result sets to objects.
  • Support for getting rid of N+1 queries.
  • A Transaction Manager
  • iBATIS will provide transaction management for database operations if no other transaction manager is available.
  • iBATIS will use external transaction management (Spring, EJB CMT, etc.) if available.
  • Great integration with Spring, but can also be used without Spring (the Spring folks were early supporters of iBATIS).
What isn’t iBATIS ?
  • An ORM
  • Does not generate SQL
  • Does not have a proprietary query language
  • Does not know about object identity
  • Does not transparently persist objects
  • Does not build an object cache
Essentially, iBatis is a very lightweight persistence solution that gives you most of the semantics of an O/R Mapping toolkit, without all the drama. In other words ,iBATIS strives to ease the development of data-driven applications by abstracting the low-level details involved in database communication (loading a database driver, obtaining and managing connections, managing transaction semantics, etc.), as well as providing higher-level ORM capabilities (automated and configurable mapping of objects to SQL calls, data type conversion management, support for static queries as well as dynamic queries based upon an object’s state, mapping of complex joins to complex object graphs, etc.). iBATIS simply maps JavaBeans to SQL statements using a very simple XML descriptor. Simplicity is the key advantage of iBATIS over other frameworks and object relational mapping tools

No comments:

Post a Comment