s2/java/09-Data Access Layer

Source: Internet
Author: User

Persistence: is the mechanism by which data in a program is transformed between the transient state and the persistent state. The main persistence operations include saving, deleting, modifying, reading, and finding.

DAO (dataaccessobjects, data Access object ) refers to the implementation of access to persisted data between business logic and persisted data.

In the object-oriented design process, there are some " routines " to solve specific problems, called patterns. The DAO pattern provides an interface for accessing the operations required by a relational database system, separating the data access from the business logic and providing an object-oriented data access interface to the upper layer.

as you can see from the DAO pattern above, the advantage of the DAO pattern is that he achieves two isolation.

1. Data access code and business logic code are isolated. The business logic code calls the DAO method directly and does not completely feel the existence of the database table. The Division of labor is clear, the data access layer code changes do not affect the business logic code, which conforms to the principle of single function, reduce the coupling, improve the reusability.

2. Different database implementations are isolated. With interface-oriented programming, if the underlying database changes, such as SQL Server becomes Oracle, as long as the new implementation class of the DAO interface is added, the original SQL Server implementation does not have to be modified. This is in line with the " open - close " principle, which reduces the coupling of the code and improves the extensibility of code and the portability of the system.

A typical DAO pattern consists mainly of the following parts.

1. DAO Interface: Define all the operations of the database as abstract methods, can provide a variety of implementations.

2. DAO Implementation class: the concrete implementation of DAO interface definition method is given for different database.

3. Entity class: Used for storing and transmitting object data.

4. Database Connection and Shutdown tool class: Avoid the reuse of database connection and shutdown code, and make it easy to modify.

Benefits of tiered development:

1, each layer focus on the realization of their own functions, to improve quality. Different levels of attention are different, the data access layer is mainly database access, the business logic layer is focused on business logic, the front-end presentation layer focused on the layout and aesthetics of the page. According to the needs of different levels by the most suitable technical personnel to achieve, thereby improving the quality of development.

2, facilitate the Division of labor and Cooperation, so as to improve efficiency. Once the interfaces between the various levels are defined, each developer's task is confirmed, and the developers at different levels can do their job and go hand in hand, greatly speeding up the development process.

3, convenient for code reuse. Once each module has a unified external interface defined, it can be called by each module without duplicating the same functionality. For example, different business logic modules can be reused if they need to operate on the same database without having to develop corresponding DAO modules.

4, easy to program extension. For example, theDAO pattern uses interface-oriented programming, the underlying database changes, can be solved by increasing the new implementation of the interface, the implementation of non-destructive substitution, rather than modify the original code, easy to extend the program.

Features of layering:

1, each layer has its own responsibility. Hardware is responsible for storage, computing, communication, and the operating system is responsible for the management of hardware, application software work on the operating system, to achieve business functions, to meet user needs.

2, the previous layer does not care about the next layer of implementation details, the previous layer through the next layer provided by the external interface to use its function. Application software does not need to know how the operating system manages hardware, and the operating system does not need to care about the specific production process of the hardware.

3, the previous layer of the next layer of call function, the next layer can not invoke the previous layer of functionality. The next layer provides services to the previous tier rather than the services provided on the previous tier.

Principles of Layering:

1, the principle of encapsulation: in short, it is each level to provide an open unified interface, and hide the internal function implementation details, other levels can not and need not understand its internal details.

2. Sequential Access principle: The next layer provides services to the previous tier without using the services provided on the previous tier. The business logic layer can access the capabilities of the data access layer, and the data access layer cannot access the business logic layer functionality.

Note: interfaces provided to the business logic layer should not contain data access details. For example, you cannot directly return a ResultSet object, but you want to convert the relational table-to-object inside the DAO, returning it as a collection object. For connection strings,SQL statements, file paths, and so on, are provided directly at the data access layer, rather than passing through the business logic layer.

Characteristics of the Entity class:

1, the attributes of the entity class are generally decorated with private .

2, according to business needs and packaging requirements for the attributes of the entity class to provide a getter/setter method, responsible for reading and assigning properties, general use of public decoration.

3, the entity class to provide a non-parametric construction method, according to business needs to provide a corresponding parameter construction method.

4, entity class best implement java.io.Serializable interface, should define attribute Serialversionuid, solve the problem of serialization between different versions. For example:

Private static final long serialversionuid=2070056025956126480l;

s2/java/09-Data Access Layer

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.