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 2 of 3

Spring - iBatis integration example


In this section, I?ll show you how to use Spring for iBatis. I?m assuming that you are already familiar with Spring. If you are new to Spring or just need a refresher, I recommend that you have a look at Spring tutorial.

This brief example will take you through a walkthrough of a typical use of SQL Maps as well as Spring ? iBatis integration. The easiest place to start is to analyze what you?re working with. What are your business objects? What are your database tables? How do they relate to each other? For our example, consider the following simple Trade class that conforms to the typical JavaBeans pattern.


Trade.java

 package com.developersBook.iBatis.domain;

public class Trade {  
 private long tradeId;
    private String action;
    private String symbol;
    private long shares;
    private double price;
    private String state;

    public String getAction() {
        return action;
    }
    public void setAction(String action) {
        this.action = action;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public long getShares() {
        return shares;
    }
    public void setShares(long shares) {
        this.shares = shares;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public long getTradeId() {
        return tradeId;
    }
    public void setTradeId(long tradeId) {
        this.tradeId = tradeId;
    }
    public String toString() {
        return ("TRADE " + tradeId + ": " + action + ", " + symbol + ", "
                + shares + " shares, $" + price + "/share (" + state + ")");
    }
}
 

Let?s use the following simple table which would be suitable for a table-per-class relationship:


 CREATE TABLE TRADE (
TRADE_ID NUMBER (5, 0) NOT NULL,
SIDE VARCHAR (10) NOT NULL,
SYMBOL VARCHAR (6) NOT NULL,
SHARES NUMBER (4, 2) NOT NULL,
PRICE NUMBER (4, 2) NOT NULL,
STATE VARCHAR (20) NOT NULL,
PRIMARY KEY (TRADE_ID)
)

Once we?re comfortable with the classes and tables we?re working with, the best place to start is the Spring configuration file. The configuration file is an XML file (applicationContext.xml). Within it we will configure properties, JDBC DataSources. It gives you a nice convenient location to centrally configure your DataSource which can be any number of different implementations. The framework can handle a number of DataSource implementations including iBATIS SimpleDataSource, Jakarta DBCP (Commons), and any DataSource that can be looked up via a JNDI context (e.g. from within an app server).










applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName"
             value="oracle.jdbc.driver.OracleDriver" />        
<property name="url"
            value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
        <property name="username" value="scott" />
        <property name="password" value="tiger" />
    </bean>

    <bean id="sqlMap"
         class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>classpath:SqlMapConfig.xml</value>
        </property>
        <property name="dataSource" ref="dataSource" />
    </bean>    

<bean id="tradingService"
        class="com.developersBook.iBatis.service.TradeServiceImpl">
        <property name="sqlMapClient" ref="sqlMap" />
    </bean>
</beans>


Don�t worry too much about the tradingService details right now. Soon, I�ll explain more about what is going on here. Observe the Spring configuration xml,the sqlMap property must be wired with a reference to an iBATIS SqlMap-Client. In Spring, the best way to get an SqlMapClient is through SqlMapClientFactoryBean:


  <bean id="sqlMap"
         class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>classpath:SqlMapConfig.xml</value>
        </property>
        <property name="dataSource" ref="dataSource" />
 </bean>
 

SqlMapClientFactoryBean is a Spring factory bean that produces an SqlMapClient.The dataSource property is wired with a reference to a javax.sql.DataSource.


Defining iBATIS SQL maps

As for the configLocation property, it should be configured with the path to an XML file that enumerates the locations of the iBATIS SQL maps. For the trade application, I�ve defined one SQL map file per domain object. Therefore, the sql-map-config.xml file will look like this:


SqlMapConfig.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE
sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL MAP Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    <settings useStatementNamespaces="true"/>
    <sqlMap resource="TradingSQLMap.xml"/>
</sqlMapConfig>

The SQL map file is loaded as resources from the classpath under the same package as the domain object.



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.