Detailed explanation of design process under MVC + DAO design mode
DAO design:
The DAO layer is mainly responsible for the data persistence layer. Some tasks that are responsible for communicating with the database are encapsulated here. The design of the DAO layer first is to design the interface of DAO, then define the implementation class of the interface in the configuration file of Spring, and then call the interface to do the data service in the module without paying attention to the specific implementation class of this interface. The structure is very clear. The data source configuration of Dao layer and the parameters related to database connection are configured in the spring configuration file.
In this layer, the establishment of object relationship mapping is mainly completed. Through this mapping, the access to the database can be realized by accessing the business object, so that there is no need to write complex database access programs with SQL statements in the development, which simplifies the access to the database and improves the development efficiency. At the same time, through the configuration of object relationship mapping, complex relationships between business objects can be established, such as one to many, many to one, one to one, many to many and so on. In this way, it is no longer necessary to establish complex relationships between tables in the database, which separates the relationship between business objects from the database, and simplifies the establishment and maintenance of the database. Hibernate framework is mainly used in this layer.
To solve the above problems, a Dao pattern (data access object) based on MVC pattern model layer is generated, which is mainly composed of factory class, proxy class, implementation class (daoimpl), Dao interface and value object class (VO) and database connection class.
Here is a summary of the design process of MVC + Dao. Through the design pattern of MVC + Dao, the structure of the project can be more clear in the design process, and can be easily modified. MVC is a hierarchical model, that is, model, view and controller. Dao is a database access model that isolates database operations.
Environmental description:
Database: MySQL
Development language: JSP + servlet + java
Server: Tomcat 7 x
Package planning:
Entity places the entity class corresponding to the table in the database. Dao places the interface to access the database in DAO design mode Impl places the interface implementation class servlet corresponding to Dao
Util Toolkit
Overview of design process:
0. Design database and view page (view) 1 Design database tool class 2 Design entity class (model) conforming to Java Bean standard 3. Design Dao interface for accessing database 4. Design implementation class of Dao interface 5. Create servlet response request (controller)
Example: take a simple login page design as an example
0. Design database and view page
Database design:
Page view:
index. The core code of JSP is as follows:
The core code is a form form, which is used to provide a view and an interface for users to input. The core is to specify the action and method attributes. This should be the simplest step, and the following work enters the real coding stage.
Forwarding page:
message. JSP core code is as follows: in fact, it's just a sentence
1. Design database tools
The operation of this step should be similar. The purpose is to extract public code and simplify the program flow.
dbConfig. The properties file stores the database configuration file. The advantage of this is that it can easily modify the relevant information of the database configuration after the project is compiled.
Dbutil class design: this class is used to realize the public operation of establishing database connection and closing database connection.
The code is as follows:
Note here: all imported packages are Java SQL package. Two core methods are established here. Getconn () and closeall () methods are often used later to obtain and close database connections respectively.
2. Design the entity class conforming to the Java Bean standard
The entity class here corresponds to the admin table above. Therefore, the admin class is designed as follows:
The entity class is designed to encapsulate data. Just compare it with the database design, and then it's best to comply with the design standard of Java beans and access it with getter / setter.
3. Design the Dao interface to access the database
The design of Dao interface is very simple. The purpose is to provide a template for the following specific business methods.
The admindao interface is as follows:
4. Design and implement the implementation class of Dao interface
Next, the implementation class of Dao interface above is designed to implement specific business. This can reflect the role of the above template.
Dao implementation class implements specific business methods and encapsulates SQL related operations.
5. Create a servlet to respond to the request
The servlet handles the request.
Here, the login method is independent of the dopost method, which helps to expand the function.
What is introduced here is only the design process of MVC + Dao. Through this mode, the design of the project can be improved to make the project structure clearer. With reasonable methods, the specific project will not be too chaotic. Finally, let's talk about the areas that need to be improved:
1. Database connection control can be improved by using connection pool, such as DBCP or c3p0.2 Database operations can be improved with common dbutils
summary
The above is all about the detailed explanation of the design process under MVC + DAO design mode. I hope it will be helpful to you. Welcome to: code sharing of login interceptor implemented by spring MVC, introduction to asynchronous upload method implemented by spring MVC using multipartfile, source code analysis of spring MVC after startup, etc. you can leave a message at any time to point out any problems. Thank you!