[JavaWeb] Java three-tier architecture to write a login case

1. What is the three-tier architecture?

The Java background is equivalent to a Java server, and its function is to connect the database and the front end (or browser or web page).

How is it connected? That is a three-tier architecture:
Example: pandas is a NumPy-based tool created to solve data analysis tasks.

①Web layer

Also called the presentation layer, it deals with the front end.

②Service layer
is also called the business layer, which is used to process specific business logic. The data is queried from the database, but sometimes it needs to be processed.
These business logics are written in the business layer.

③Dao layer
Also called data access layer, the full name is Data access object, data access object, that is, dealing with the database, the learned Jdbc is the code in this layer.
So the whole process is: query the data from the database, then process it through specific business logic, and then return it to the web layer to be rendered into a web page, and finally it can be directly viewed by the user.
The package in which the code written is stored also has a specification:
insert image description here

insert image description here

①The beans package
is also a JavaBean. To put it bluntly, a table in the corresponding database is a class, and how many tables need to be operated corresponds to as many classes.

②The dao package
is the above-mentioned data access layer, which is used to access and manipulate the addition, deletion, modification and query of data.

③The service package
is the above-mentioned business logic layer, which is used to process business logic.

④The utils package
is a package that stores tool classes, such as a Jdbc tool class JdbcUtil that was customized during this period.

⑤The web package
is the above-mentioned display layer, which is used to display data on the front end.

2. Write a login case

I think that when completing a requirement, it is best to analyze it first and make a macro plan. How to do this requirement, the idea must be clear first. In fact, writing code is a very fast thing, and the important thing is the idea, so It is said to clarify the thinking first, write a process, and then do it.

1. Web layer
insert image description here

①Simulate the login page
Because I haven't learned the front-end knowledge yet, I use the Scanner class to simulate a login page.

② Call the service layer to realize the login operation
There is a method in the service layer called login(), the parameters are the user name and password in ①, and the return value is the data queried from the database.

③ Judgment result
If the return value does not exist, that is to say, the user name or password entered by the user cannot be queried in the database.
Then the user is prompted: the user name or password is incorrect.

2. Service layer
insert image description here
Because the example of the login case is very simple, there is no specific business logic, just need to query the database, so the code writing is also very simple, just call the method of Dao layer to query the database, the method is as follows The meaning of the name:
queryUserByNameAndPwd() is to query the user according to the name and pwd.

3. Dao layer
insert image description here
According to the user name and password entered in the web layer, the corresponding data is queried from the database, that is, the Jdbc code is written, and the JdbcTemplate is used here.

2. Code testing

After the code is written, do a test.
insert image description here

①Test one

The user name and password do exist in the data table, so the login is successful.

②Test 2

The username and password are incorrect, so the login fails. The above is the preliminary study of the three-tier architecture and the writing of a simple login case. perfectly.

Guess you like

Origin blog.csdn.net/wang_qiu_hao/article/details/125223723