SSM framework introduction

Spring framework

Introduction

Spring is known as J2EE spring, is an open source lightweight Java development framework with Inversion of Control (IoC) and aspect-oriented (AOP) two core. The Java Spring framework flexibly manages transactions in a declarative manner to improve development efficiency and quality. Official website document

The Spring framework is not limited to server-side development. From the perspective of simplicity, testability, and loose coupling, any Java application can benefit from Spring. The Spring framework is also a super glue platform. In addition to providing its own functions, it also provides the ability to glue other technologies and frameworks. Spring is a container, and all objects in the container will have these services and functions provided by Spring.

Architecture

Insert picture description here

advantage

  1. Convenient decoupling and simplified development.
    Spring is a big factory that can delegate the creation of all objects and the maintenance of dependencies to Spring.
  2. Convenient integration of various excellent frameworks
    Spring does not exclude various excellent open source frameworks, and provides direct support for various excellent frameworks (such as Struts2, Hibernate, MyBatis, etc.).
  3. Reducing the difficulty of using Java EE API
    Spring provides encapsulation for some very difficult APIs (JDBC, JavaMail, remote calls, etc.) in Java EE development, which greatly reduces the difficulty of applying these APIs.
  4. Convenient program testing
    Spring supports JUnit4, and you can easily test Spring programs through annotations.
  5. AOP programming support
    Spring provides aspect-oriented programming, which can easily implement functions such as program permission interception and operation monitoring.
  6. Support for declarative transactions
    Only need to configure to complete the management of the transaction, without manual programming.

concept

The container
Spring contains and manages the configuration and life cycle of application objects. In this sense, it is a kind of container. You can configure how each of your beans is created-based on a configurable prototype (prototype), your beans can be created A single instance or a new instance every time it is needed-and how they are related to each other. However, Spring should not be confused with traditional heavyweight EJB containers. They are often bulky and bulky and difficult to use.

Lightweight
Spring is lightweight in terms of size and overhead. The complete Spring framework can be published in a JAR file of only 1MB in size. And the processing overhead required by Spring is also negligible. In addition, Spring is non-intrusive: Typically, objects in Spring applications do not depend on Spring's specific classes.

Inversion of Control
Spring promotes loose coupling through a technique called Inversion of Control (IoC). When IoC is applied, other objects that an object depends on will be passed in in a passive way, instead of creating or finding dependent objects by the object itself. You can think of IoC as the opposite of JNDI-it's not that the object looks for dependencies from the container, but the container actively passes the dependencies to it when the object is initialized without waiting for the object's request.

Aspect-oriented
Spring provides rich support for aspect-oriented programming, allowing cohesive development by separating application business logic and system-level services (such as auditing and transaction management). Application objects only implement what they are supposed to do-complete business logic-nothing more. They are not responsible (or even aware of) other system-level concerns, such as logging or transaction support.

SpringMVC

Introduction

Spring MVC is a part of Spring. Spring Web MVC is a Java-based lightweight web framework that implements the request-driven type of the Web MVC design pattern. It uses the idea of ​​the MVC architecture pattern and decouples the responsibilities of the web layer. Request-driven refers to the use of a request-response model. The purpose of the framework is to help us simplify development. Spring Web MVC is also to simplify our daily web development.

advantage

Clear role division: front controller (DispatcherServlet), request to processor mapping (HandlerMapping), processor adapter (HandlerAdapter), view resolver (ViewResolver), processor or page controller (Controller), validator (Validator) , Command object (the object to which the Command request parameter is bound is called the command object), and form object (the object provided by the Form Object for form display and submission is called the form object).

The division of labor is clear, and the extension point is quite flexible, which can be easily extended, although it is almost unnecessary;

Since the command object is a POJO, there is no need to inherit the framework-specific API, and the command object can be used directly as a business object;

Seamless integration with other Spring frameworks, which other web frameworks do not have;

Adaptable, any class can be supported as a processor through HandlerAdapter;

Customizable, HandlerMapping, ViewResolver, etc. can be easily customized;

Powerful data verification, formatting, and binding mechanism;

The Mock object provided by Spring can be used to perform Web layer unit testing very simply;

The support of localization and theme analysis makes it easier for us to perform internationalization and theme switching.

There are also support such as RESTful style support, simple file upload, contract programming support where the convention is greater than the configuration, and zero configuration support based on annotations.

MVC design model

M : Model (model): data model, which provides the data to be displayed, so it contains data and behavior, which can be considered as a domain model or JavaBean component (including data and behavior), but it is generally separated now: Value Object (data) And the service layer (behavior). That is, the model provides functions such as model data query and model data status update, including data and business.

V : View: Responsible for the display of the model, which is generally the user interface we see and what customers want to see. In the past, JSP technology was mainly used. Now the template engine is generally used if the front and back ends are not separated, and vue is used if the front and back ends are separated.

C : Controller (controller): Receives user requests, delegates to the model for processing (state change), and returns the returned model data to the view after processing, and the view is responsible for display. In other words, the controller does the job of a dispatcher.

MVC is a well-known design pattern, especially in the field of Web applications. The patterns are all about decoupling the module containing the business data from the view of the display module. How did this happen? How can a view (for example, a JSP page) be decoupled from its model (for example, a JavaBean containing data)? Remember this saying? A level of redirection can solve almost all problems in the computer industry. Indeed, introducing a redirection layer between the model and the view can solve the problem. This redirection layer is the controller. The controller will receive the request, perform the operation to update the model, and then notify the view about the model change. A controller that depends on the state of the model and depends on the request can decide which view to display.

Principle of operation

Insert picture description here


The specific process steps of the core architecture are as follows:

First, the user sends a request——>DispatcherServlet. After receiving the request, the front controller does not process it by itself, but delegates to other resolvers for processing, as a unified access point for global process control;

DispatcherServlet——>HandlerMapping, HandlerMapping will map the request to a HandlerExecutionChain object (including a Handler processor (page controller) object, multiple HandlerInterceptor interceptors) object, through this strategy mode, it is easy to add a new mapping strategy;

DispatcherServlet——>HandlerAdapter, HandlerAdapter will package the processor as an adapter to support multiple types of processors, that is, the application of the adapter design pattern, so it is easy to support many types of processors;

HandlerAdapter——>Call of the processor function processing method, HandlerAdapter will call the real processor function processing method according to the adaptation result to complete the function processing; and return a ModelAndView object (including model data, logical view name);

The logical view name of ModelAndView -> ViewResolver, ViewResolver will resolve the logical view name into a specific View. Through this strategy mode, it is easy to change other view technologies;

View——>Rendering, View will render according to the incoming Model model data. The Model here is actually a Map data structure, so it is easy to support other view technologies;

The control right is returned to DispatcherServlet, and DispatcherServlet returns a response to the user, and this process ends.

Mybatis and MybatisPlus

Mybatis

The concept of data persistence

Data persistence is a general term for converting the data model in memory to a storage model and converting the storage model to a data model in memory. For example, file storage, data reading, etc. are all data persistence operations. The data model can be any data structure or object model, XML, binary stream, etc.

When we write an application to operate the database and perform the operation of adding, deleting, modifying, and checking table data, it is the operation of data persistence.

Introduction

MyBatis framework is an open source data persistence layer framework. It internally encapsulates the operation of accessing the database through JDBC, supports ordinary SQL queries, stored procedures and advanced mapping, and almost eliminates all manual settings of JDBC codes and parameters and retrieval of result sets. As a persistence layer framework, MyBatis's main idea is to strip out a large number of SQL statements in the program and configure them in the configuration file to achieve flexible configuration of SQL. The advantage of this is that SQL is separated from the program code, and SQL can be modified directly in the configuration file without modifying the code.

MyBatis uses simple XML or annotation methods for configuration and original mapping to establish a mapping relationship between entity classes and SQL statements. It is a semi-automatic (the reason why it is semi-automatic, because we have to write SQL ourselves) ORM implementation.

advantage

  1. Compared with JDBC, the amount of code is reduced by more than 50%.
  2. MyBatis is the best persistence layer framework, small and easy to learn.
  3. MyBatis is quite flexible and will not impose any impact on the existing design of the application or database. SQL is written in an XML file, completely separated from the program code, reducing coupling, facilitating unified management and optimization, and being reusable.
  4. Provide XML tags, support the writing of dynamic SQL, to meet different business needs.
  5. Provides mapping tags to support the mapping of ORM fields between objects and databases.

Disadvantage

  1. The workload of writing SQL statements is relatively large, and there are certain requirements for the ability of developers to write SQL.
  2. SQL statements depend on the database, which leads to the lack of portability of the database, and the database cannot be replaced casually.

Applications

MyBatis focuses on SQL itself and is a sufficiently flexible DAO layer solution. For projects that have high performance requirements or changes in requirements, such as Web projects, MyBatis is the best choice.

MybatisPlus

ORM

ORM (Object/Relational Mapping) refers to object-relational mapping, which is a data persistence technology. It directly establishes the corresponding relationship between the object model and the relational database, and provides a mechanism to manipulate the data in the database table through the JavaBean object.

Insert picture description here


In our actual development, we use object technology to operate the database, but when storing data, we use a relational database, which causes a lot of inconvenience. ORM builds a bridge between the object model and the tables of the relational database. With ORM, we no longer need to use SQL statements to manipulate tables in the database, and use APIs to directly manipulate JavaBean objects to achieve data addition, deletion, modification, and query operations.

Introduction

Mybatis-Plus (MP for short) is an enhancement tool for Mybatis. On the basis of Mybatis, it only enhances and does not change. It is born to simplify development and improve efficiency. This is the official definition. For more introduction and features of mybatis-plus , please refer to the official website of mybatis-plus . So how is it enhanced? In fact, it has already encapsulated some crud methods, we don't need to write xml anymore, just call these methods directly, just like JPA.