“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

preface

Hello everyone, I'm gw_Gw. I'm glad to learn and progress with you. Copy the codeCopy the code

The following content is from the network, if there is any infringement, please contact me to delete, this article is only used for learning exchange, not for any commercial purposes.

Abstract

This article mainly introduces the development of web procedures when the basic architecture and the role of each layer and contact, hope beginners less detour.Copy the code

JavaWebApplication architecture

1. Basic concepts

When doing Web development, we must first determine the technology selection and architecture design, using layering can make it easier for us to carry out collaborative development, improve work efficiency. Although many beginners understand the function and relationship of each layer after searching, but really do not know how to design when it comes to use (I am the same). Today let’s talk about the layers of web application development, what each layer does, and how they are connected.

First, take a look at the following image:

The diagram above represents the basic layers, but let’s explain what each layer does and how they relate to each other.

  1. Dto layer: entity class layer, which stores a table in the database (the name of the table is the name of the class, the field of the table is the attribute, and each attribute has the corresponding set, get method), and we should create the first layer.
  2. Dao layer: Data persistence layer, used to operate DTO, actually encapsulates various basic operations on DTO (add, delete, check, change, etc.).
  3. Web layer: The Web layer is divided into servlet layer (actual business logic processing), filter layer (filtering page), listener layer (listening).
  4. Service layer: The service layer, by invoking the basic operations of the DAO layer, can be combined to perform more complex operations for use by the Servlet layer.
  5. Utils layer: Holds the utility classes you need to use.

2. Practical application

Now that we know the functions and connections of each layer, let’s reinforce the impression with examples.

【 】 dto layer

public class User {
​
  private long id;
  private String name;
  private String gender;
 
  public long getId() {
    return id;
  }
​
  public void setId(long id) {
    this.id = id;
  }
​
​
  public String getName() {
    return name;
  }
​
  public void setName(String name) {
    this.name = name;
  }
​
​
  public String getGender() {
    return gender;
  }
​
  public void setGender(String gender) {
    this.gender = gender;
  }
​
Copy the code

【 】 the dao layer

Encapsulates basic operations (CRUD, etc.) on classes in dtos.

We typically define the interface first and then the implementation class.

Public interface UserDao {** ** query all User information ** @return User information */ List<User> findAll(); /** * add user ** @param user */ void add(user user); /** * @param id */ void delete(int id); ** @param parseInt */ User findUserById(int parseInt); /** * @param user */ void update(user user);Copy the code

Then we can write the implementation class according to the specific requirements.

Typically we put the implementation classes under the Impl package.

【 】 the service layer

The same is true of the Service layer, where we define the interface first and then the implementation class.

Public interface UserService {/** * findAll(); public interface UserService {/** * findAll(); /** * addUser * @param user */ void addUser(user user); /** * deleteUser * @param id */ void deleteUser(String id); * @param ID * @return */ User findUserById(String id); /** * modify user information * @param user */ void updateUser(user user); }Copy the code

Then write the implementation classes as required, and again we’ll put the implementation classes under the Impl package.

The discovery is similar to the DAO layer in that it encapsulates the DAO layer again by combining the dao layer’s operations to provide more complex functionality.

【 】 web layer

The most important part of the Web layer is the servlet, which is our business logic. We can design filters and listeners as needed.

summary

The above is about the role of each layer of web program development and contact, I hope to help readers, if there is a wrong place, welcome to comment correction, only see not practice or easy to forget, if still can not understand, strongly suggest that they follow a completed program to knock again. Sleep well leave zan ha.