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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public class Customer { private Long id; private String firstName; private String lastName; public Customer() { } public Long getId(){ return id; } public void setId(Long id){ this.id = id; } public String getFirstName(){ return firstName; } public void setFirstName(String firstName){ this.firstName = firstName; } public String getLastName(){ return lastName; } public void setLastName(String lastName){ this.lastName = lastName; } public String toString(){ return "Id = " + id + ", First Name = " + firstName + ", Last Name = " + lastName; } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public interface CustomerRepository { public List getAllCustomers(); public Customer getCustomerById(String customerId); public void insertCustomer(Customer newCustomer); public void deleteCustomer(String customerId); public void updateCustomer(Customer existingCustomer); } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap namespace=”loyalty”> <typeAlias type = "com.mybook.sample.Customer" alias = "customer"/> <resultMap class = "customer" id = "result"> <result property = "id" column = "id"/> <result property = "firstName" column = "first_name"/> <result property = "lastName" column = "last_name"/> </resultMap> <select id = "getAllCustomers" resultMap = "result"> select * from customer </select> <select id = "getCustomerById" resultMap = "result" parameterClass = "string"> select * from customer where id = #value# </select> <insert id = "insertCustomer" parameterClass="customer"> insert into customer (id, first_name, last_name) values (#id#, #firstName#, #lastName#) </insert> <delete id = "deleteCustomer" parameterClass="string"> delete from customer where id = #value# </delete> <update id = "updateJsr" parameterClass="jsr"> update customer set first_name = #firstName#, last_name = #lastName# where id = #id# </update> </sqlMap> |
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
1 2 3 4 5 6 7 8 |
<?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> <sqlMap resource="./spring/ibatis/Customer.xml" /> </sqlMapConfig> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import org.springframework.orm.ibatis.SqlMapClientTemplate; public class IBatisCustomerRepository implements CustomerRepository { private SqlMapClientTemplate sqlMapClientTemplate; public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) { this.sqlMapClientTemplate = sqlMapClientTemplate; } public SqlMapClientTemplate getSqlMapClientTemplate() { return sqlMapClientTemplate; } public List getAllCustomers() { return (List) sqlMapClientTemplate. queryForList("getAllCustomers"); } public Customer getCustomerById(String customerId) { Object obj = sqlMapClientTemplate.queryForObject( "getCustomerById", customerId); return obj instanceof Customer ? ((Customer)obj) : null; } public void insertCustomer(Customer newCustomer) { sqlMapClientTemplate.insert("insertCustomer", newCustomer); } public void deleteCustomer(String customerId) { sqlMapClientTemplate.delete("deleteCustomer", customerId); } public void updateCustomer(Customer existingCustomer) { sqlMapClientTemplate.update( "updateCustomer", existingCustomer); } } |
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
1 2 3 4 5 6 |
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/loyalty"/> <property name="username" value="loyalty"/> <property name="password" value="password"/> </bean> |
b. SqlMapClientFactoryBean configuration – the main class in iBatis whch does the job is configured as a bean Listing 8-49. SqlMapClientFactoryBean bean configuration
1 |
./spring/ibatis/SqlMapConfiguration.xml |
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
1 2 3 4 5 6 7 8 |
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="sqlMapClient"> <ref local="sqlMapClient"/> </property> </bean> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public class SpringIBatisTestingClass { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml", SpringIBatisTestingClass.class); Customer customer = new Customer(); customer.setId(“123”); customer.setFirstName(“john”); customer.setLastName(“smith”); CustomerRepository customerRepository = (CustomerRepository)ac.getBean(“customerRepository”); System.out.println("Inserting Customer"); customerRepository.insertCustomer(customer); System.out.println("Listing all Customer’s"); List allCustomers = customerRepository.getAllCustomers(); for (Customer aCustomer : allCustomers) { System.out.println(aCustomer); } System.out.println("Selecting a Customer by Id '123'"); Customer createdCustomer = customerRepository.getCustomer(“123”); System.out.println(createdCustomer); } } |
Page Visitors: 10454

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
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 .
simple, well written. keep writing..