Implementing database Programming with Java -08 DAO pattern

Source: Internet
Author: User
Tags list of attributes serialization

1. data Persistence: The mechanism that transforms the data in the program between the instantaneous state and the persistent state is the data persistence;

2. Persistent Implementation: Database, ordinary file, XML file;

3. JDBC encapsulation: Using interface-oriented programming, can reduce the coupling between the code;

To improve the maintainability and extensibility of the code, we use JDBC to encapsulate the data:

First, define the unified API, the code of Operation data into the interface, the business logic code only need to call the implementation class object of these interfaces, can realize the access to the data,

In order to isolate the implementation details and adopt interface-oriented programming, we can reduce the coupling between the code and improve the extensibility and maintainability of the code.

Advantages:

  • Isolate business logic code and data access code

isolate the implementation of different databases

reasons for encapsulation: business code and data access code coupling: Poor readability, not conducive to late modification and maintenance, not conducive to code reuse;

4 , DAO: role: DAO acts as a converter, converting entity classes to records in a database;

DAO Model Summary:

DAO (data access Objects) data Access objects, located between business logic and persistence, enable access to persistent data,

In layman's words, it is to encapsulate the data operation and provide the corresponding interface to the outside.

In the object-oriented design process, there are some routines to solve problems, called patterns,

DAO mode provides an interface for accessing the operations required by a relational data system, separating data access from business logic, and providing an object-oriented data access interface to the upper layer .

As can be seen from the DAO pattern, it achieves a two-layer separation.

The data access code and the business logic code are isolated, the business logic code calls the DAO method directly, completely does not feel the existence of the data table, the division of labor is clear, the data Access layer code does not affect the business logic layer code, which also conforms to the single function principle, reduces the coupling degree and improves the reusability of the code.

isolate the implementation of different databases, using interface-oriented programming, if the underlying data changes, such as MySQL becomes Oracle. Need to increase the implementation of the DAO interface class can be, the original MySQL implementation class without modification, which conforms to the opening and shutting principle, reduce coupling, improve scalability and portability

A typical DAO pattern consists mainly of the following parts.

DAO interface: Defines all operations on the database as abstract methods, which are placed inside the interface, and can provide multiple implementations

DAO Implementation classes: Implementations of different DAO interface-defined methods for different databases (different implementation classes)

entity class: Used to store the transmitted object data, directly passing the object on the line, do not pass many parameters

Database connection Close Tool class: There are some such as add, delete, re-use code can be extracted into the public class, as well as close and connect the database, to avoid code redundancy ...

Very popular data access mode--dao mode

Data Access object

Between business logic and persisted data

Implementing access to persistent data

DAO part of the pattern

DAO Interface

DAO Implementation Class

Entity class

Database connection and Shutdown tool classes

4. Properties config file: Let the user modify the related variable settings from the program itself--using the configuration file;

prpoerties classes: Classes that can read Java configuration files, we write common configuration information in the configuration file, easy to maintain, read.

Properties configuration file: Java configuration file is generally the end of the properties. The format is a key = value pair, which can be annotated with #,

Add Properties Profile step: Src folder right-----NEW-----File-----Output Properties end of configuration file.

to add configuration information to the file:
Eg:

driver=com.mysql.jdbc.driver                   // Add load-driven string

url=jdbc:mysql://localhost:3306/epet          // Add a linked string, localhost can also be an IP address, 3306 is the port number, Epet is the database name

username=epetadmin                          // database user name

password=root                              // database user password

N the configuration file in Java is often the properties file

the U suffix is. properties

U format is "key = value" Format

U use "#" to annotate

The Properties class is provided in Java to read the configuration file :

Method name

Say    

String GetProperty (strin G key)

Search for properties in this property list with the specified key. The corresponding value is obtained by the parameter key

Object SetProperty (String key, String value)

Call Hashtable method put. Set key-value pairs by calling the base class's put () method

void Load (InputStream in Stream)

reads the list of attributes (key and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file

void Clear ()

clears the loaded key-value pair, which is provided by the base class Hashtable

6 , using entity classes to pass data: Data access code and business logic code transfer data through the entity class class, the related information is encapsulated into the entity class, the program passes the entity class as the parameter of the method.

Characteristics of entity classes

entity class general properties using private decoration

According to business requirements and packaging requirements of the entity class Getter/setter method, responsible for reading and assigning attributes, general use of public decoration

provide parameterless constructors for entity classes and provide parametric construction methods based on business requirements.

entity class best Implement Java.io.Serializable interface, support serialization mechanism, can convert object to byte serialization and save to disk (hard disk) or network transmission.

If the entity class implements the Serializable interface. You should define the attribute Serialversionuid to resolve different versions of the serialization problem ...

Implementing database Programming with Java -08 DAO pattern

Related Article

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.