ArticlesTutorialsInterview Questions & AnswersCertificationsFree Books & Sample ChaptersTechnical Discussions  Your Account | Advertise with us | Feedback |
Few pages of this site are under construction and we are constantly trying to improve it. If you have any suggestions or ideas about how to improve the site, please Let us know.
iBatis Tutorial

Contents
  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:


TradingSQLMap.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL MAP 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd"
>
<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"/>
    </cacheModel>


    <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"/>
    </resultMap>

    <select id="getTrade" parameterClass="long" resultMap="tradeResult"
cacheModel
="tradeCache">
        SELECT
               TRADE_ID,
               SIDE,
               SYMBOL, | Declares
               SHARES, | getTrade
               PRICE, | query
               STATE
        FROM
              TRADE
        WHERE
              TRADE_ID = #id#
    </select>

<!-- 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">
        UPDATE
              TRADE
        SET
              STATE = 'EXECUTED' | Declares
        WHERE | executeTrade(update)
              TRADE_ID = #id# | query
    </update>

</sqlMap>


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.

ITradeService.java

 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.

TradeServiceImpl.java

 package com.developersBook.iBatis.service;

import
com.developersBook.iBatis.domain.Trade;
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.


IBatisClient.java

 package com.developersBook.iBatis.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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(
                    "applicationContext.xml");
            System.out.println("Classpath loaded");
            System.out.println(" ");
            ITradeService service = (ITradeService) ctx
                    .getBean("tradingService");            
// getTrade
            int tradeId = 1;
            System.out.println("BEFORE: " + service.getTrade(tradeId));

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


Conclusion

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.


Resources


  • 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 >>>>
Test

  ALSO SEE :

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.