DAO mode,

Source: Internet
Author: User

DAO mode,
JDBC Encapsulation

Advantages:

 

Note:

The DAO mode provides interfaces required to access the relational data system, separating data access from the business logic.

The upper layer provides an object-oriented data access interface.

 

DAOMode implements two-layer separation:

 

  • The division of labor between codes is clear. The code at the data access layer does not affect the code at the business logic layer. This also conforms to the single functional principle, which reduces coupling and improves code reusability ..
  • Isolate the implementation of different databases and adopt interface-oriented programming. If the underlying data changes, for example, mysql becomes oracle. You need to add the DAO interface implementation class. The original Mysql implementation class does not need to be modified. This complies with the open and closed principles, reduces coupling, and improves scalability and portability.

 

DAOMain Components of the Mode:

 

  • DAO interface: define all database operations as abstract methods and put them in the interface to provide multiple implementations.
  • DAO implementation class: provides implementation of methods defined by different DAO interfaces for different databases (different implementation classes)
  • Object Class: used to store transmitted object data, you can directly pass the object without passing many parameters
  • Database connection close tool class: There are some reusable code such as adding, deleting, and modifying which can be extracted to the public class, as well as shutting down and connecting to the database to avoid code redundancy ..
  • Right-click SRC → NEW → File → output the configuration File ending with properties.
Prpoerties Properties configuration file

 

  • Add configuration information to the file

 

Example:

 

Driver = com. mysql. jdbc. Driver // Add the Url of the Driver to be loaded = jdbc: mysql: // localhost: 3306/epet // Add the link string

Username = epetAdmin // database Username

Password = root // Database User Password

Read configuration files

PropertiesCommon Methods

 

Method

Description

String getProperty (String key)

Obtain the value with the specified key.

Object setProperties (String key, String value)

Call the put Method of hashTable and set the key-Value Pair by calling the put () method of the base class.

Void load (InputStream instr)

Reads the attribute list from the input stream and obtains the key value of the file by loading the specified file.

Void clear ()

Clears the reprinted key-value pair. This method is provided by hashtable.

Transmit data using entity classes

The data access code and the business logic code transmit data through the entity class to encapsulate the relevant information into the entity. In the class, the program uses the entity class as a method parameter for transmission.

Entity features
  • General Attributes of object classes are modified using private
  • The getter/setter method is used to read and assign values to attributes based on business requirements and encapsulation requirements. This method is generally modified using public.
  • Provides constructors without parameters for the object class and parameter-based constructor based on business needs.
  • It is best for the entity class to implement the java. io. Serializable interface and support the serialization mechanism. The object can be converted to byte serialization and saved to the disk (hard disk) or transmitted over the network.
  • If the object class implements the serializable interface. The attribute serialVersionUID should be defined to solve serialization problems of different versions.

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.