ArticlesTutorialsInterview Questions & AnswersCertificationsFree Books & Sample ChaptersTechnical Discussions  Your Account | Advertise with us | Feedback |
iBatis Tutorial

  1. iBatis Introduction

  2. Spring - iBatis Configuration

  3. Spring - iBatis Integration Example
  4. Resources

iBatis Tutorial

                     By Anand , developersBOOK.COM , 12/01/07

Page 3 of 3

Now that we have a DataSource configured and our central configuration file is ready to go, we will need to provide the actual SQL Map file which contains our SQL code and the mappings for parameter objects and result objects (input and output respectively). Continuing with our example above, let?s build an SQL Map file for the Trade class and the TRADE table. We?ll start with the general structure of an SQL document, and a simple select statement:


 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-// SQL MAP 2.0//EN"
<sqlMap namespace="trading">

    <typeAlias alias="trade" type="com.developersBook.iBatis.domain.Trade"/>
    <cacheModel id="tradeCache" type="LRU" readOnly="true">
        <flushInterval hours="24"/>
        <flushOnExecute statement="trading.executeTrade"/>
        <property name="cache-size" value="1000"/>

    <resultMap id="tradeResult" class="trade">
        <result property="tradeId" column="TRADE_ID"/>
        <result property="action" column="SIDE"/> | Defines
        <result property="symbol" column="SYMBOL"/> | result
        <result property="shares" column="SHARES"/> | mapping
        <result property="price" column="PRICE"/>
        <result property="state" column="STATE"/>

    <select id="getTrade" parameterClass="long" resultMap="tradeResult"
               SYMBOL, | Declares
               SHARES, | getTrade
               PRICE, | query
              TRADE_ID = #id#

<!-- Use Trade object (JavaBean) "id" properties as parameters for update. Each of the
        parameters in the #hash# symbols is a JavaBeans property. -->

    <update id="executeTrade" parameterClass="long">
              STATE = 'EXECUTED' | Declares
        WHERE | executeTrade(update)
              TRADE_ID = #id# | query


Now that we are all configured and mapped, all we need to do is code it in our Java application. Let�s start with service layer.

 package com.developersBook.iBatis.service;

import com.developersBook.iBatis.domain.Trade;

public interface ITradeService {

    public Trade getTrade(long tradeId)throws Exception ;
    public void executeTrade(long tradeId) throws Exception; 

TradeServiceImpl class implements ITradeService interface.

 package com.developersBook.iBatis.service;

import com.ibatis.sqlmap.client.SqlMapClient;

public class TradeServiceImpl implements ITradeService {

protected SqlMapClient sqlMap = null;

    public void setSqlMapClient(SqlMapClient sqlMap) {
        this.sqlMap = sqlMap;

    public Trade getTrade(long tradeId) throws Exception {
        return (Trade)sqlMap.queryForObject("trading.getTrade", tradeId);

    public void executeTrade(long tradeId) throws Exception {
        sqlMap.update("trading.executeTrade", tradeId);

Let's code the client to test this Spring - iBatis integration.

 package com.developersBook.iBatis.client;

import org.springframework.context.ApplicationContext;
import com.developersBook.iBatis.service.ITradeService;

public class IBatisClient {
    public static void main(String[] args) {
        try {
            System.out.println("IBatisClient started");
            System.out.println(" ");

            // load spring beans
            ApplicationContext ctx = new ClassPathXmlApplicationContext(
            System.out.println("Classpath loaded");
            System.out.println(" ");
            ITradeService service = (ITradeService) ctx
// getTrade
            int tradeId = 1;
            System.out.println("BEFORE: " + service.getTrade(tradeId));

            // update trade
            System.out.println("AFTER : " + service.getTrade(tradeId));
            System.out.println(" ");
            System.out.println("Hurry!!!! Its done!");
        } catch (Exception e) {


One size does not fit all the java persistence and the important to know there are other solutions besides the traditional ORMs, and that would be iBatis. After reading this tutorial and exploring the demonstration application, you should now have a better appreciation for what iBATIS has to offer and when you would want to use it in your work. With its low barriers to entry, transparent utilization of SQL, cleanly divided separation of responsibilities, and elegant integration with Spring, the strengths of iBATIS within today's computing environment are self-evident.


  • You can download the source code of this tutorial  here.
  • iBATIS homepage.
  • To learn more about Spring Framework, see the Spring tutorial .
  • You can find the differences between iBatis and Hibernate here.
  • Browse for books on iBatis and other technical topics.
  • Discuss about this tutorial here.

<<<<Previous | 1 2 3 |Next >>>>


About Us | Our Mission | Terms of use | Author Terms |    Search        | Contact Us | Advertise With Us | Link to Us | Sitemap
Copyright © 2016 developersBOOK.COM. All rights reserved.