Home  >  Article  >  Database  >  DAO mode in mysql

DAO mode in mysql

一个新手
一个新手Original
2017-10-10 09:54:042055browse

JDBC Encapsulation

Advantages:

  1. Isolate details

  2. Reduce coupling between codes

  3. Improve code scalability and maintainability

##Note:

##              ##DAO mode provides an interface for operations required to access relational data systems, separates data access from business logic, and provides an object-oriented data access interface to the upper layer.

The DAO

pattern achieves two-layer separation:


The division of labor between codes is clear, and the data access layer code does not affect the business logic layer code, which is also consistent with The single-function principle reduces coupling and improves code reusability. .

  • Isolates the implementation of different databases and adopts interface-oriented programming. If the underlying data changes, such as mysql becomes oracle. You only need to add the implementation class of the DAO interface. The original Mysql implementation class does not need to be modified. This is in line with the opening and closing principle, reduces coupling, and improves scalability and portability.

  • # #DAO

    Main components of the pattern:

DAO interface: Define all operations on the database as abstract methods and place them in the interface. A variety of implementations can be provided

    DAO implementation class: Implementation of different methods defined by the DAO interface (different implementation classes) for different databases
  • Entity class: used to store the transferred object data, just pass the object directly without passing many parameters
  • Database connection closing tool class: There are also some such as adding, Deleted and modified reused code can be extracted into public classes, and the database can be closed and connected to avoid code redundancy.
  • Right-click on SRC→NEW→File→Output the end of properties configuration file.
  • Prpoerties class
  • Properties configuration file

Add configuration information in the file


  • Example:

  • Driver=com.mysql.jdbc.Driver                   //添加加载驱动的字符串Url=jdbc:mysql://localhost:3306/epet                                   //添加链接的字符串
    Username=epetAdmin                                      //数据库用户名
    Password=root                                             //数据库用户密码
Read configuration file

Properties

Common methods of class

MethodGet the value with the specified keyCall the put method of hashTable and set it by calling the put() method of the base class Key-value pairsRead the attribute list from the input stream, by Load the specified file to obtain the key value of the fileClear the reproduced key value Yes, this method is provided by hashtable. Classes transmit data and encapsulate relevant information into entities; in classes, programs pass entity classes as parameters of methods. Defining entities based on business needs and encapsulation requirements The class performs getter/setter methods and is responsible for reading and assigning attributes. Generally, the public modification

Description

##String getProperty(String key)

Object setProperties(String key,String value)

Void load(InputStream instr)

Void clear()

Characteristics of entity classes

General attributes of entity classes are modified with private


is used to provide a parameterless constructor for the entity class and a parameterized constructor according to business needs. .

Entity classes should preferably implement the java.io.Serializable interface and support the serialization mechanism. The object can be converted into byte serialization and saved to disk (hard disk) or transmitted over the network. .

  • If the entity class implements the serializable interface. You should define the attribute serialVersionUID to solve the serialization problem of different versions.

The above is the detailed content of DAO mode in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn