Sunday, October 18, 2009

Chapter 8: Spring JDBC Support



















Chapter 8: Spring JDBC Support




Overview



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 databases are perhaps MySQL (www.mysql.com) and PostgreSQL (www.postgresql.org). MySQL is very fast, but it lacks support for more advanced features such as stored procedures and an internal scripting language. On the other hand, it runs on almost any operating system and platform. PostgreSQL is a bit pickier when it comes to the host platform, but it supports lots of advanced features. It is also worth noting that PL/pgSQL is very close to Oracle's PL/SQL.


Even if you choose the fastest and most reliable database, you cannot afford to loose 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 off 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 convenience 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 DataSources you can use in your applications.





  • Mapping the data to Java objects: We show you how to effectively and easily 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.



























No comments:

Post a Comment