Spring in Action Learn notes -- Fourth Chapter Conquer database
First, a good news, during the 2006 National Day, Spring2.0 Official edition (Http://www.springframework.org/download) and Spring2.0 Chinese documents (http:// spring.jactiongroup.net/viewtopic.php?t=2279&sid=0906ae6a913537b249e501db5b54c181) was finally released.
After the previous study, we've learned the core of the spring framework, and in this chapter the author tells us about the persistence layer of spring, and introduces us to Srping's support for JDBC. The reader is also briefed on how spring integrates with some of the current ORM frameworks, such as Hibernate, JDO, OJB, and Ibatis.
First, Learning Spring of the DAO Philosophy
The DAO represents a data Access object. It perfectly describes the role of DAO in an application. The existence of DAO provides a way to read and write data in a database. As long as this function is exposed through the interface, other parts of the application can access the database through this interface. The following illustration shows the appropriate way to design a data access layer:
As shown in the figure above, service objects are no longer set together with specific data access implementations, making them easy to test. In addition, we access the data access layer in a way that is unrelated to the persistence technology, and the DAO interface does not need to expose what technology he uses to access the data. One way that spring helps you isolate the data access layer from the rest of the application is to provide a consistent hierarchical anomaly system that runs through the entire DAO framework.
1 . Understanding Spring 's DataAccessException
Spring's DAO framework does not throw exceptions that are related to a particular technology, and spring provides a convenient way to convert an exception that is specific to a technique, such as SqlException, into its own exception, which belongs to the DataAccessException level.
DataAccessException is runtimeexception, so it is an exception that requires no detection. It means that when they are thrown by the data access layer, you do not need your code to handle such exceptions. This follows the general idea of spring: anomaly checking makes your code cluttered with unrelated catch or throws statements. This is especially true for data access exceptions, because we are always flooded with exceptions in the code that writes the connection database and operates database tables.
In general, our database access API always throws a meaningful exception. JDO has its own level of anomaly. The following figure is the level of exception defined in Spring2.0.
The exception classes that are included in the above diagram are just a subset of the entire large dataaccessexception anomaly hierarchy.
2 . Consistent DAO support
No matter what technology we use, some database access steps are required. such as database connection, operation, after operation to release the connection. These are all fixed steps. Spring separates the fixed and mutable parts of the database access process into two distinct classes: templates (Template) and callbacks (Callback). Template management process of the fixed part, and in the back of the mediation fill in the implementation details. As shown in the following illustration:
In the diagram above, spring's template class handles the invariant part of data access-transaction control, resource management, and exception handling. The implementation of the callback interface defines the application-specific part-creating statement, bonding parameters, and organizing the result set (ResultSet). So we just need to be concerned with the logic of data access.
To facilitate the use of various database access technologies in a consistent manner, such as JDBC, JDO, and Hibernate,spring provides a set of abstract DAO classes for us to extend. These abstract classes provide methods through which we can obtain data sources and other configuration information related to the current data access technology.
These classes are:
L JDBCDAOSUPPORT–JDBC the base class for data access objects.
L HIBERNATEDAOSUPPORT–HIBERNATE the base class for data access objects.
L JDODAOSUPPORT–JDO the base class for data access objects.
L JPADAOSUPPORT–JPA the base class for data access.
For the above four categories, please refer to chapter tenth of Spring2.0 for more detailed information.
Second, in Spring used in JDBC
At first we used JDBC when we used the Java Connection database, and at first it was possible to do one operation on the database table and connect the database once, then the database connection was closed. This is probably the step: connect the database--------------close the database, then some people encapsulate the operation of the database, and put the database connection operation close to a class (http://blog.csdn.net/qutr/archive/2006/09 /25/1274826.aspx