Spring Data persistence

Source: Internet
Author: User

1, Spring provides a set of data access framework, integrated with a variety of data access technology. Support for JDBC, Ibatis, Hibernate, JPA, and other persistence frameworks.

2. Spring's philosophy of data access

One of the goals of spring is to allow developers to follow the "programming of Interfaces" in object-oriented principles when developing applications. Spring's support for data access is no exception. DAO is the abbreviation for the data Access object, which provides a way for data to be read and written to the database. They publish functionality as an interface, and other parts of the application can be accessed through an interface.

The service object accesses DAO through an interface, which makes the service objects easy to test because they are no longer tied to a particular data access implementation. In addition, the data Access layer is accessed in a way that is not related to persistence technology. The persistence option is independent of DAO, and only the relevant data access methods are published through the interface. This allows for a flexible design and minimizes the impact of the switching persistence framework on other parts of the application. If the implementation details of the data access layer are infiltrated into other parts of the application, then the entire application will be coupled with the data access layer, leading to a rigid design.

3. Spring's data access anomaly system

JDBC accesses the data to force the capture of the SqlException exception, and JDBC throws a SqlException exception for all data access problems, but the exception does not tell you what went wrong and how to handle it, and you can no longer solve the problem in the catch block. Some persistence frameworks provide a relatively rich system of exceptions. correspond to specific data access issues, respectively. This allows you to write a catch code block for the exception you want to handle.

Unlike Jdbc,spring, which provides multiple data access exceptions, it describes the problem they are throwing.

Although spring's anomaly system is much richer than the simple sqlexception of JDBC, it is not associated with a particular persistence pattern. This means that we can throw a consistent exception with spring, without worrying about the chosen persistence scheme. This helps us isolate all persistence mechanisms from data access. Spring believes that many of the problems that trigger an exception cannot be fixed in a catch code block. Instead of forcing developers to write catch code blocks, Spring uses non-checked exceptions. This leaves the developer with the power to catch exceptions.

4. Templating of data access

Spring explicitly divides the fixed and variable parts of the data access process into two different classes: template and Callback (callback). The template manages the fixed part of the process while the callback handles the custom data access code.

Spring provides several optional templates for different persistence platforms.

Using a data access template, you simply configure it as a bean in the spring context and weave it into the application's DAO. Alternatively, you can use spring's DAO support classes to further simplify the DAO configuration of your application. Although it is a good choice to directly weave templates, spring also provides a number of convenient DAO base classes that can be used to manage templates.

5. Using DAO support class

The data access template is not all of the spring data access framework. Each template provides a few easy ways to simplify data access without having to create explicit callback implementations. In addition, based on the template-callback design, Spring provides the DAO support class and the business's own DAO class as its subclass.

Spring not only provides multiple data template implementation classes, but also provides a corresponding DAO support class for each template.

6. Configure the data source

Regardless of which spring DAO you choose to support, you need to configure a reference to a data source. Spring provides several ways to configure the data source Bean in the spring context, including:

1) data sources defined through the JDBC driver;

2) Data sources that are found through Jndi;

3) The data source of the connection pool;

  

Spring Data persistence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.