Considerations for Using JDBC
From the previous discussions, you can see how Spring can make your life much easier when using
JDBC to interact with the underlying RDBMS. However, there is still quite a lot of code you need to
develop, especially when transforming the resultset into the corresponding domain objects.
On top of JDBC, a lot of open source libraries have been developed to help close the gap between
the relational data structure and Java's OO model. For example, MyBatis (formerly known as iBATIS) is a
popular DataMapper framework that is also based on SQL mapping. MyBatis lets you map objects with
stored procedures or queries to an XML descriptor file (Java annotation is supported too). Like Spring,
MyBatis provides a declarative way to query object mapping, greatly saving you the time it takes to
maintain SQL queries that may be scattered around various DAO classes.
There are also many other ORM frameworks that focus on the object model, rather than the query.
Popular ones include Hibernate, EclipseLink (also known as TopLink), and OpenJPA. All of them comply
with the JCP's JPA specification.
In recent years, those ORM tools and mapping frameworks have become much more mature so that
most developers will settle on one of them, instead of using JDBC directly. However, in cases where you
need to have absolute control over the query that will be submitted to the database for performance
purposes (e.g., using a hierarchical query in Oracle), Spring JDBC is really a viable option. And when
using Spring, one great advantage is that you can mix and match different data access technologies. For
example, you can use Hibernate as the main ORM and then JDBC as a supplement for some of the
complex query logic or batch operations; you can mix and match them in a single business operation and
then wrap them under the same database transaction. Spring will help you handle those situations easily.
This chapter showed you how to use Spring to simplify JDBC programming. You learned how to connect
to a database and perform selects, updates, deletes, and inserts, as well as call stored functions. How to
use the core Spring JDBC class, JdbcTemplate, was discussed in detail. In addition, we covered other
Spring classes that are built on top of JdbcTemplate and that help you model various JDBC operations.
In the next few chapters, we will discuss how to use Spring with popular ORM technologies when
developing data access logic.
Search WWH :