ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Meindert" <meind...@pastelebusiness.com>
Subject converting iBATIS framework DAOs to the Spring Framework
Date Thu, 12 Apr 2007 10:03:43 GMT

Hi all.


I've documented the process of converting jpetshop5, and would like to know
if it is right?

Also I don't know what to do with the com.ibatis.dao.client.DaoException
thrown in SequenceSqlMapDao?

I'm going to add the transaction on OrderService now.

 


Steps to convert jpetshop5 to ibatis-spring 


 

The DAO iBATIS has been depreciated and it is recommended to replace this
with the spring framework <http://www.springframework.org/> . This document
describes the steps taken to convert from ibatis dao to spring dao. I work
on the project in netbeans so some terminology used might be incorrect for
other IDE's

 

Prerequisite is to have a project similar to jpetstore5, and have it
connected to a 'normal' database

(There is support for hssqldb in the DaoConfig class what we are going to
throw away.)

 


Lib jar files;


Remove the old ibatis jar files from the class path and create the reference
to the new ibatis and spring jars

*	Remove ibatis-common-2.jar
*	Remove ibatis-dao-2.jar
*	Remove ibatis-sqlmap-2.jar
*	Add ibatis-2.3.0.677.jar
*	Add spring.jar
*	Add commons-dbcp-1.2.1.jar
*	Add commons-pool-1.2.jar
*	Add commons-collections.jar

 


Com.ibatis.jpetstore.persistence.sqlmapdao


Change the BaseSqlMapDao to;

package com.ibatis.jpetstore.persistence.sqlmapdao;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

public class BaseSqlMapDao extends SqlMapClientTemplate {

    protected static final int PAGE_SIZE = 4;

}

All the other SqlMapDao files;

Remove the public AccountSqlMapDao(DaoManager daoManager)  methode

Remove import com.ibatis.dao.client.DaoManager;


Com.ibatis.jpetstore.service


The DAO will be injected by spring, thus the constructor can be deleted

Example for AccountService, remove the public AccountService()methode

(also remove import com.ibatis.dao.client.DaoManager;)

There is a transaction in OrderService.java, this transaction must be
handled by Spring, remove it here for now. Also remove it out of the
constructor;

public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao
sequenceDao) {

    this.itemDao = itemDao;

    this.orderDao = orderDao;

    this.sequenceDao = sequenceDao;

  }

 


The Link between spring and ibatis


Add the SpringInit.java class to a new package com.listeners

This class will be instantiated on load of the website and will pass the
reference to the spring managed objects


Com.ibatis.jpetstore.presentation


 

The presentation layer must connect with the spring managed service layer

So replace;

public AccountBean() {

    this(new AccountService(), new CatalogService());

  }

With

public AccountBean() {

     this((AccountService)
SpringInit.getApplicationContext().getBean("accountService"), 

          (CatalogService)
SpringInit.getApplicationContext().getBean("catalogService"));

  }

And add import com.listeners.SpringInit;

Repeat for the other classes

 


Configuration


First setup the listeners to have SpringInit initiated on loading of the
site

Add to web.xml (after <description> tag)

<context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/spring.xml</param-value>

</context-param>

    

<listener>

 
<listener-class>org.springframework.web.context.ContextLoaderListener</liste
ner-class>

</listener>

    

<listener>

 <listener-class>com.listeners.SpringInit</listener-class>

</listener>

 

As you can see it is looking for /WEB-INF/spring.xml create this file.
Attached is the one I created for jpetstore5

 


Cleanup


Throw away DaoConfig.java and dao.xml out of
com.ibatis.jpetstore.persistence, and the reference to it in the Service
classes

 


Mime
View raw message