Show Table of Contents
Chapter 4. Data Access with Spring
If you are using transactions in your application, you will inevitably also be accessing some persistent resources. Spring provides a variety of APIs to support programmatic access to persistent resources and you might find it helpful to familiarize yourself with these data access APIs. In particular, this chapter describes Spring's JDBC API in some detail.
4.1. Programming Data Access with Spring Templates
To provide access to various kinds of persistent storage, Spring encapsulates the relevant API in a template class. The purpose of the template class is to provide a simplifying wrapper layer around each type of storage and to ensure that any required Spring features are integrated cleanly with the persistence layer.
Spring provides the following template classes for data access:
The org.springframework.jms.core.JmsTemplate class is a general-purpose class for managing Java Messaging Service (JMS) connections. One of the main advantages of this class is that it simplifies the JMS synchronous access codes.
To create an instance of a
JmsTemplate, you need to supply a reference to a javax.jms.ConnectionFactory object.
The org.springframework.jdbc.core.JdbcTemplate class is a wrapper around a JDBC data source, enabling you to access a JDBC database using SQL operations.
To create an instance of a
JdbcTemplate, you need to supply a reference to a javax.sql.DataSource object (for example, see Section 2.6.1, “JDBC Data Source”).
For a detailed discussion of the
JdbcTemplateclass, see Section 4.2, “Spring JDBC Template”.
The org.springframework.jdbc.core.simple.SimpleJdbcTemplate class is a convenience wrapper around the
JdbcTemplateclass. This class has been pared down so that it includes only the most commonly used template methods and it has been optimized to exploit Java 5 features.
The org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate class is a convenience wrapper around the
JdbcTemplateclass, which enables you to use named parameters instead of the usual
?placeholders embedded in a SQL statement.
The org.springframework.orm.ibatis.SqlMapClientTemplate class is a simplifying wrapper around the iBATIS
SqlMapClientclass. iBATIS is an Object Relational Mapper (ORM) that is capable of automatically instantiating Java objects based on a given SQL database schema.
The org.springframework.orm.hibernate3.HibernateTemplate class provides an alternative to working with the raw Hibernate 3 session API (based on sessions returned from
For Hibernate versions 3.0.1 or later, the Spring documentation recommends that you use the native Hibernate 3 API instead of the
HibernateTemplateclass, because transactional Hibernate access code can now be coded using the native Hibernate API.
The org.springframework.orm.jpa.JpaTemplate class provides an alternative to working with the raw JPA
The Spring documentation now recommends that you use the native JPA programming interface instead of the
JpaTemplateclass. Considering that the JPA programming interface is itself a thin wrapper layer, there is little advantage to be had by adding another wrapper layer on top of it.