Integrating Spring and iBatis

iBatis is an object-relational mapping tool (ORM) that simplifies access to database. This article details the steps needed for integrating iBatis with Spring. Through such an integration, objects that are specific to iBatis can utilise all the benefits given by Spring's IOC Container. This is not an introductory article for both Spring and iBatis Frameworks. First-time readers are encouraged to read the Introductory article for Spring in javabeat Introduction to Spring Web Framework to know the preliminary concepts related to Spring.

2) Step-by-Step Procedure for Integration

2.1) Introduction

We are going to create a sample table in the MySql Database and going to access the data within it using Spring-iBatis Integration. The required bundles needed to build and run the sample program are listed below.

  • Spring Distribution
  • MySql Database
  • MySql Database Driver
2.2) Creating tables

Create a table called Jsr which contains relevant information for holding information like name, id, description andspecification lead for a Java Specification Request (JSR). Issue the following command in the MySql Client command prompt to create the table,


create table Jsr (JsrId varchar(10), JsrName varchar(50), JsrDescription, varchar(500), SpecLead varchar(100));


2.3) Creating the Java Equivalent


Now let us create a equivalent Java class for the Jsr table. This class, will contain properties that will map to the column names in the Jsr table. Given here is the complete code listing for the Jsr Java class,



Jsr.java




package javabeat.net.articles.spring.ibatis;

public class Jsr
{
private String id;
private String name;
private String description;
private String specLead;

public String getId()
{
return id;
}

public void setId(String id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getDescription()
{
return description;
}

public void setDescription(String description)
{
this.description = description;
}

public String getSpecLead()
{
return specLead;
}

public void setSpecLead(String specLead)
{
this.specLead = specLead;
}

public String toString()
{
return "Id = " + id + ", Name = " + name +
", Description = " + description + ", Lead = " + specLead;
}
}


2.4) JsrDao Class


Then, we need to get into the client-facing Dao interface design. This is the interface that clients will be depending on, to perform various database operations like selection of rows, insertion, deletion, updating data etc.



JsrDao.java




package javabeat.net.articles.spring.ibatis;

import java.util.List;

public interface JsrDao
{

public List<Jsr> selectAllJsrs();
public Jsr selectJsrById(String jsrID);

public void insertJsr(Jsr insertJsr);
public void deleteJsr(String jsrId);
public void updateJsr(Jsr jsrWithNewValues);

}


2.5) iBatis Mapping File


Jsr.xml




<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap>

<typeAlias type = "javabeat.net.articles.spring.ibatis.Jsr" alias = "jsr"/>

<resultMap class = "jsr" id = "result">
<result property = "id" column = "JsrId"/>
<result property = "name" column = "JsrName"/>
<result property = "description" column = "JsrDescription"/>
<result property = "specLead" column = "SpecLead"/>
</resultMap>

<select id = "selectAllJsrs" resultMap = "result">
select * from Jsr
</select>

<select id = "selectJsrById" resultMap = "result" parameterClass = "string">
select * from Jsr where JsrId = #value#
</select>

<insert id = "insertJsr" parameterClass="jsr">
insert into Jsr (JsrId, JsrName, JsrDescription, SpecLead) values (#id#, #name#, #description#, #specLead#)
</insert>

<delete id = "deleteJsr" parameterClass="string">
delete from Jsr where JsrId = #value#
</delete>

<update id = "updateJsr" parameterClass="jsr">
update Jsr set JsrName = #name#, JsrDescription = #description#, SpecLead = #specLead#
where JsrId = #id#
</update>

</sqlMap>


iBatis mapping file contains the mapping information between a Java class and its corresponding table in the database. Not only does it contain this mapping information, but also it contains many definitions for Named Queries. A Named Query is just a query defined with some name so that it can be reused across multiple modules.



The above Xml file starts with an element called 'typeAlias' which is just a short-name for'javabeat.net.articles.spring.ibatis.Jsr'. Instead of referencing the fully-qualified name of the Jsr class, now it can be shortly referred as 'jsr' in the other sections of the Xml file. Next comes the mapping information specified in the form of 'resultMap' element where the associations between the Java properties for the corresponding column names are made.



Then, the Named Queries section follows. A query called 'selectAllJsrs' has been defined which is actually a select query. The query string value is manifested in the form of 'select * from Jsr'. By having such a query definition, it can be used elsewhere in the Application just by referring the query identifier. Now, let us choose a query definition that illustrates passing parameters to it. The query identifier 'selectJsrById' needs the JsrId as a parameter using which it can filter the number of rows fetched. This can be represented by using the attribute'parameterClass'. Here 'string' stands for java.lang.String which means that the parameter is of type String. Similarly there are values like 'int', 'float', etc for java.lang.Integer and java.lang.Float respectively. Inside the query definition, we have the following query string,




select * from Jsr where JsrId = #value#


In the above query string, we have defined a new symbol called 'value'. This is the default symbol name for the parameter and since we have only one parameter it would not cause any problem. The expression '#value#' will be substituted with the values specified at the run-time. (Later we will see how the value gets substituted to the above expression).



Now, let us see a query definition that accepts multiple parameters. In the query definition 'insertJsr', we want the jsr id, jsr name, jsr description and spec lead values to get inserted and we have defined the query string as follows,




insert into Jsr (JsrId, JsrName, JsrDescription, SpecLead) values (
#id#, #name#, #description#, #specLead#)


In the query definition, the value of the parameter value is pointing to 'jsr', which means that during run-time the query string will get translated as follows,




insert into Jsr (JsrId, JsrName, JsrDescription, SpecLead) values (
jsr.getId(), jsr.getName(),jsr.getDescription(), jsr.getSpecLead())



Technorati Tags: ,



LiveJournal Tags: ,
+