Topic 8
■■■
Spring JDBC Support
In the previous chapters, you saw how easy it is to build a fully Spring-managed application. You now
have a solid understanding of bean configuration and Aspect-Oriented Programming (AOP)--in other
words, you know how to wire up the entire application using Spring. However, one of the parts of the
puzzle is missing: how do you get the data that drives the application?
Apart from simple throwaway command-line utilities, almost every application needs to persist data
to some kind of data store. The most usual and convenient data store is a relational database.
The most notable open source relational databases are perhaps MySQL (www.mysql.com) and
PostgreSQL (www.postgresql.org). In terms of RDBMS features being provided, both databases are about
the same. MySQL is generally more widely used for web application development, especially on the
Linux platform. On the other side, PostgreSQL is friendlier to Oracle developers, because its procedural
language, PL/pgSQL, is very close to Oracle's PL/SQL language.
Even if you choose the fastest and most reliable database, you cannot afford to lose the offered
speed and flexibility by using a poorly designed and implemented data access layer. Applications tend to
use the data access layer very frequently; thus, any unnecessary bottlenecks in the data access code
impact the entire application, no matter how well-designed it is.
In this chapter, we show you how you can use Spring to simplify the implementation of data access
code using JDBC. We start by looking at the horrendous amount of code you would normally need to
write without Spring and then compare it to a data access class implemented using Spring's data access
classes. The result is truly amazing--Spring allows you to use the full power of human-tuned SQL
queries while minimizing the amount of support code you need to implement. Specifically, we will
discuss the following:
Comparing traditional JDBC code and Spring JDBC support: We explore how
·
Spring simplifies the old-style JDBC code while keeping the same functionality.
You will also see how Spring accesses the low-level JDBC API and how this low-
level API is mapped into convenient classes such as JdbcTemplate.
Connecting to the database: Even though we do not go into every little detail of
·
database connection management, we do show you the fundamental differences
between a simple Connection and a DataSource. Naturally, we discuss how Spring
manages the DataSources and which data sources you can use in your applications.
Retrieving and mapping the data to Java objects: We show you how to retrieve data
·
and then effectively map the selected data to Java objects. You also learn that
Spring JDBC is a viable alternative to object-relational mapping (ORM) tools.
Inserting, updating, and deleting data: Finally, we discuss how you can implement
·
the insert, update, and delete operations so that any changes to the database you
are using do not have a devastating impact on the code you have written.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home