Spring Book – Chapter 8 – Data Access

Spring and iBatis

While Hibernate is certainly an excellent ORM technology, it is not always the right choice for developing all different kinds of application that we have today. In other words, Hibernate works very well if your data model is completely (most part) in sync with object model, because the crux of ORM solutions is to map object to tables. Suppose if the data model is not in sync with object model, the ORM way of working doesn’t actually work and the model starts breaking down. This is where use iBatis comes into picture as de-facto alternative. iBatis maps results sets to objects, so you don’t have to take care about table structure. It works well with stored procedures, reporting applications, etc.

The listings in this section show the actual Spring and iBatis SQL maps integration in action.  Some steps are identical to Hibernate steps but I am repeating it as is so that the reader doesn’t have to turn pages and loose the flow of reading. The various steps involved are:

1. Database table creation – create the table according to the database that you are using. Since we have taken Customer entity for explaining this integration example, create the customer table with all the relevant columns. For ease of understanding, I will be using only three fields in the Customer entity namely ‘First Name’, ‘Last Name’ and the primary key ‘ID’. Step same as Hibernate.

2. POJO (Customer) – we now need to create a class called as Customer which will be used as java equivalent for the Customer table which exists in the database. Step same as Hibernate. It is a simple POJO and the class is shown in Listing 8-43  below:

Listing 8-43. Customer POJO class equivalent to Customer table

3. CustomerRepository interface – the base interface which the implementation class will implement. Listing 8-44 shows the various methods in this interface.

Listing 8-44. CustomerRepository interface

4. iBatis mapping file – file which contains the mapping information between a Java class and its corresponding table in the database. It also contains all the named queries required for the DAO/Repository.

Listing 8-45. iBatis mapping file – Customer.xml

5. iBatis configuration file – file containing references to various mapping files. We will name this file as SqlMapConfiguration.xml.

Listing 8-46. iBatis configuration file – SqlMapConfiguration.xml

6. CustomerRepository/CustomerDao implementation class – here comes the actual implementation of the CustomerRepository implementation class using iBatis SQL maps. We will have a new implementation of CustomerRepository interface based on iBatis and will call it as IBatisCustomerRepository.

Listing 8-47. IBatisCustomerRepository implementation class

7. Spring configuration file – the file is called applicationContext.xml.

a. DataSource configuration – one of the important beans which require mention. Listing 8-48 shows datasource bean configuration which uses MySql database.

Listing 8-48. Spring DataSource bean configuration

b. SqlMapClientFactoryBean configuration – the main class in iBatis whch does the job is configured as a bean Listing 8-49. SqlMapClientFactoryBean bean configuration

c. SqlMapClientTemplate configuration – the teamplate class used for iBatis is called SqlMapClientTemplate. Configuring this class as bean to be used in the repository is done in here.

Listing 8-50. SqlMapClientTemplate bean configuration

d. CustomerRepository/CustomerDao configuration – the IBatisCustomerRepository class configuration as a Spring bean. Listing 8-51. CustomerRepository bean configuration
8. Testing class –dummy testing class to show the integration between Spring and iBatis. Ideally you will have a service class which calls the repository class in your application. Listing 8-52. Testing class showing Spring and iBatis in action

Page Visitors: 10654

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 8 – Data Access

  1. Hi my friend! I wish to say that this article is awesome, nice written and come with approximately all important infos. Iˇ¦d like to peer more posts like this .

Leave a Reply

Your email address will not be published. Required fields are marked *