Subms supermarket management system (mainly talk about MVC three-tier architecture)

(Subms) Supermarket Order Management System

  • Use technology java, Servlet, (JSP, js, jquery, css) without introduction, Tomcat server

The main exercise is how to understand the MVC three-tier architecture at a deeper level. Our supermarket order management system is actually an item of addition, deletion, modification and investigation. Mainly talk about some of the business logic and architectural ideas inside.
Insert picture description here
Briefly talk about the three-tier MVC architecture

First of all, the three-tier architecture is consistent with the goals of MVC: both are for decoupling and improving code reuse. MVC is a design pattern, and the three-tier architecture is a software architecture.

The three-tier architecture is divided into: presentation layer (UIl) (web layer) , business logic layer (BLL) (service layer) , data access layer (DAL) (dao layer) , plus entity class library (Model)

1. Entity class library (Model) , in Java, it is often called Entity entity class. The database is used to store data, and we usually choose to use a special class to abstract the structure of the data table, and the attributes of the class correspond to the attributes of the table one-to-one.
· Generally speaking, Model entity class library layer needs to be referenced by DAL layer, BIL layer and UI layer.

2. The data access layer (DAL) is mainly to store the access to data types, that is, basic operations such as adding, deleting, modifying, and updating the database. DAL is based on business requirements, constructing SQL statements, constructing parameters, invoking helper classes, and obtaining results. The DAL layer is called by the BIL layer.

3. Business logic layer (BLL) The BLL layer is like a bridge, connecting the UI presentation layer with the DAL data access layer. What is responsible is to deal with issues related to business logic, such as processing and judging data before calling and accessing the database.

The reference relationship of each layer is as shown in the figure below. The relationship between
Insert picture description here
mvc and the three-tier architecture is shown in the following figure.
Insert picture description here
Next, I will briefly describe this project around the three-tier architecture.

Project construction

  • Build a Maven web project
  • Configure Tomcat
  • Test whether the project is running
  • Import the jar packages that will be encountered in the project;
    .. jsp, servlet, mysql driver, jstl, stand,,, (dependency)
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>

<!--standard-->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    
    
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.36</version>
    </dependency>
<!--    jstl标签注解依赖-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
<!--    servlet 依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
    </dependency>

<!--    jsp依赖-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

<!--    mysql依赖 我的数据库版本是8.0.23版本不同的去官网换新的-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.23</version>
    </dependency>

-Create project package structure
Insert picture description here

  • Writing the entity class introduced above the linked list mapping is to encapsulate the fields in the database into attributes and add get and set to each field.
    Insert picture description here
  • Write basic public classes, create a db.properties
    file under resources , which is the database configuration file, and the configuration is as follows
#数据源
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?ServerTimezone=UTC
username=root
password=123456



1. Write Dao operation data empty public class

Insert picture description here
Insert picture description here
Insert picture description here
Public class end

  • To prevent garbled codes from appearing on the page, we use filters to filter garbled codes
    - Insert picture description here
    don't forget to register this filter
    Insert picture description here





2. Login function (password verification, logout, filter optimization)

        I first list the user user 's operation database layer and business layer interface and the name of the interface implementation class first (a method in the interface corresponds to a method in the implementation class for better troubleshooting and optimization)

  • UserDao (Dao layer interface)
  • UserDaoImpl (Dao layer interface implementation class)
  • UserService (Business Layer Interface)
  • UserService (business layer interface implementation class)

Verify account


UserDao
Insert picture description here

UserDaoImpl

Here is the quote
Insert picture description here

Userservice
Insert picture description here
UserserviceImpl
Insert picture description here
servlet layer interacts with the front-end layer
Insert picture description here



Logout function

        The logout function is that a servlet execution address is placed after the a tag in a jsp page, and I only list the servlet code logic.
Insert picture description here

Filter optimization

        Optimized that we cannot access the main page without logging in. Because we have to log in to access the main page. If someone copies the URL of the main page and visits directly without logging in, it won’t blow up.
Insert picture description here





3. Modify the password

Change password page
Insert picture description here

UserDao
Insert picture description here

UserDaoImpl
Insert picture description here

Userservice
Insert picture description here

UserserviceImpl
Insert picture description here
servlet层
Insert picture description here

to sum up

  1. In fact, that’s it. The future functions are meaningless. We just need to deepen the architectural thinking. The layers used in this are indeed very convoluted. The query method of executing SQL is written in the public class. Create Preparation and Result, so we have to create it on the Dao layer. Adding, deleting and modifying methods do not write the Prepare method and you need to create it yourself .

  2. Also, (Conection conection) is added to the method body in the Dao layer, but it is not implemented in the Dao layer. It is only in the Dao layer whether it exists and if it exists, then go down.

  3. According to my understanding, in the service business layer of this project, only the connection= method of connecting data in the public class is written, and then the Dao layer is called again and then the connection and other attributes are passed on to achieve a separate process. .
  4. When I wrote this project, I created a lot of bugs because of carelessness. The most important thing to note is that when the login verification is implemented, an object in the implementation class smbms_user is created in the Dao layer because it is encapsulated. The getset method is used, so here is to put all the properties of the query in this object and then return. The point is that we put this object in the setAttribute in the Servlet layer, which can easily make us freely get the parameters (getArrribute) in each servlet.

  5. The meaning of this writing is just to let us know more about the idea of ​​MVC three-tier architecture, so I did not bring the front-end jsp when describing the code inside to explain in detail, I will list a picture to briefly talk about this project Architecture
Insert picture description here

The newcomer just started to write, so forgive me that my ability to describe and write is not very good.

Guess you like

Origin blog.csdn.net/qq_42283142/article/details/114199321