Wednesday, November 5, 2008

CheckOut The iBATIS

The iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java,in Java the object are the POJO(Plan Old Java Object).The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files. By this there will be a big reduction in the amount of effort put by developer to access relational database using APIs like JDBC and ODBC. The Other Presistence framework like hibernate allow the creation of an object model (in Java) by the user, and create and maintain the relational database automatically but iBATIS works in the reverse order ,the developer starts with the Sql database and create the Java Object automatically.iBatis is a better approach when the developer dnt have full control over the SQL db schemas eg. In the case when the application need to acces an existing SQL db or the new DB schemas which is not fully under the control of devloper ( when database design team has created the schemas and to optimized against better performance.)
EXAMPLE:
Assume there is a database table STUDENT(STUD_ID INTEGER, STUD_DESC VARCHAR(64)) and a Java object com.example.Student (id: int, description: String). To read the student record having the key STD_ID into a new Product POJO, the following mapping is added into an iBatis XML mapping file:
select id="Student" parameterClass="java.lang.Long" resultClass="com.example.Student"
select STUD_ID as id,
STUD_DESC as description
from STUDENT
where STUD_ID = #value#
/select

A new java STUDENT object can retrived from db against the valid Key (stundent number 999)
Student student = (Student ) sqlMapClient.queryForObject("getStudent t", 999);
The sqlMapClient object is an instance of class com.ibatis.sqlmap.client.SqlMapClient.
In the iBatis XML mapping file example, #value# is mentioned as the long integer and the value passed into the Select query.

No comments: