Spring Book – Chapter 8 – Data Access

MappingSqlQuery

The class is present in org.springframework.jdbc.object package and aids in accessing database in a more object-oriented manner. This class is used to execute queries and get the results back as a list containing business objects with the relational column data mapped to the properties of the business object. Listing 8-26 shows a custom query that maps the data from the ‘customer’ table to an instance of the ‘Customer’ class.

Listing 8-26. Usage of MappingSqlQuery to map the table ‘customer’ to the ‘Customer’ object

StoredProcedure

Spring’s abstraction of RDBMS stored procedure is achieved using this class which is present in org.springframework.jdbc.object package. This class is abstract and it is intended that subclasses will provide a typed method for invocation that delegates to the supplied execute (…) method.

Listing 8-27. Usage of StoredProcedure which encapsulates the actual procedure

JDBC Testing Support

The org.springframework.test.jdbc package contains SimpleJdbcTestUtils, which is a collection of JDBC related utility functions intended to simplify standard database testing scenarios. Note that org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests is the JUnit support class and org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests is the TestNG support class which provides convenience methods which delegate to SimpleJdbcTestUtils internally. These classes were covered in some detail in the Chapter 7 and now you can easily relate its relevance.

JDBC Batch Operations

In any enterprise application, there will be cases in which you may have to insert a batch of records into database in one shot. If you call a single insert method for every record that you want to insert, the SQL statement will be compiled repeatedly, causing your system to have performance issues.

Listing 8-28 shows using JdbcTemplate’s batchUpdate(..) method to perform this batch customer inserts.

Listing 8-28. Usage of JdbcTemplate to perform batch insert of customers

Listing 8-29 shows the usage of NamedParameterJdbcTemplate to perform batch update of customers using named parameters in the query.

Listing 8-29. Usage of NamedParameterJdbcTemplate to perform batch update of customers using named parameters

If you would like to use the JdbcTemplate to do batch update in more than one step, Spring also provides to split your big batches into multiples and do the operation. Listing 8-30 shows doing this in multiples of 50 updates to a batch containing 5000 records of customers.

Listing 8-30. Usage of JdbcTemplate to perform batch update of customers in multiples of 50

In Listing 8-30, the batch update methods for this call returns an array of int arrays containing an array entry for each batch with an array of the number of affected rows for each update. The top level array’s length indicates the number of batches executed and the second level array’s length indicates the number of updates in that batch.

Page Visitors: 10629

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 *