The principle of the ssm framework is to integrate Spring, SpringMVC, and MyBatis as a framework for web projects with relatively simple data sources. The process is: Handler Mapping finds the corresponding Handler according to the request, calls the business logic for processing, and then performs view parsing.
SSM Framework Introduction
SSM (Spring SpringMVC MyBatis) framework set consists of two Spring and MyBatis Integrated with open source frameworks (SpringMVC is part of Spring). SSM is often used as a framework for web projects with simple data sources.
SpringMVC
SpringMVC intercepts user requests in the project. Its core Servlet, DispatcherServlet, assumes the responsibility of an intermediary and matches user requests to the Controller through HandlerMapping. The Controller is the operation performed by the specific corresponding request.
SpringMVC framework running process
The user sends the request to DispatcherServlet;
DispatcherServlet receives the request and queries one or more HandlerMapping to find and process the request Handler;
HandlerMapping finds the corresponding Handler according to the request, generates a Handler object and returns it to DispatcherServlet;
DispatcherServlet calls Handler through HandlerAdapter;
Handler (Controller) calls business logic (service) for processing, and returns to ModelAndView after processing is completed ;
HandlerAdapter returns the Handler processing result ModelAndView to DispatcherServlet;
DispatcherServlet queries one or more ViewReslover (view resolvers) and passes the ModelAndView to the specified ViewReslover;
After the ViewReslover is parsed, it returns the specific View to DispatcherServlet;
DispatcherServlet renders the View (populates the model data into the view);
DispatcherServlet responds to the user, and the View displays the results on the client.
The process is roughly as shown below:
DispatcherServlet is the core of the entire Spring MVC. It is responsible for receiving HTTP requests, organizing and coordinating the various components of Spring MVC, and its main work There are the following three items:
a. Intercept URL requests that match a specific format;
b. Initialize the WebApplicationContext corresponding to the DispatcherServlet context and associate it with the WebApplicationContext of the business layer and persistence layer;
c. Initialization Each component of Spring MVC is assembled into DispatcherServlet.
Spring
Spring is like a big factory that assembles beans in the entire project. In the configuration file, you can specify the use of specific parameters to call the constructor of the entity class. Instantiate the object. It can also be called the glue in the project.
The core idea of Spring is IOC (Inversion of Control), which means that programmers no longer need to explicitly new an object, but let the Spring framework do it all for you. The IOC container is responsible for instantiating, locating, configuring objects in the application and establishing dependencies between these objects. The purpose of Spring is to ensure that the relationship between objects (modules and modules) is not related through code, but is managed through configuration class descriptions (Spring dynamically assembles objects internally through reflection based on these configurations).
The IOC container represents the BeanFactory interface in the org.springframework.beans package, which provides the basic functions of the IOC container; and the ApplicationContext interface under the org.springframework.context package extends the BeanFactory and also provides integration with Spring AOP. , internationalization processing, event propagation and providing different levels of context implementation.
Simply put, BeanFactory provides the most basic functions of the IOC container, while ApplicationContext adds more support for enterprise-level functions. ApplicationContext fully inherits BeanFactory, so the semantics of BeanFactory also apply to ApplicationContext.
Spring running process
Load configuration file, ApplicationContext interface. The bean information in the configuration file is loaded into the HashMap. A bean usually includes id, class, property, etc. The id of the bean corresponds to the key in the HashMap, and the value in the HashMap is the bean.
Call the getBean method. getBean is used to obtain the bean in the applicationContext.xml file. The parameter is the id of the bean. In general, it will be forced to convert to the corresponding business layer (interface).
Call the method of the business layer (interface implementation).
How is the content in the bean injected? To put it simply, when a bean is instantiated, the class is actually instantiated. It calls the set method in the class through reflection to inject the class attributes previously saved in the HashMap into the class. This brings us back to the original place of Java, object.property, object.method.
Mybatis
Mybatis is an encapsulation of jdbc, which makes the underlying operations of the database transparent. Mybatis operations all revolve around a sqlSessionFactory instance. Mybatis is associated to the Mapper file of each entity class through the configuration file. The Mapper file is configured with the SQL statement mapping required for each class to the database. Every time you interact with the database, get a sqlSession through sqlSessionFactory, and then execute the sql command.
Mybatis dynamically proxies dao through MapperProxy. That is to say, when executing the method in the dao written by yourself, the corresponding MapperProxy is actually acting as the proxy.
Mybatis operating principle
Loading configuration file
Load SQL configuration information into MappedStatement objects one by one, including parameter mapping configuration, execution SQL statements, resulting mapping configuration, are stored in memory.
SQL parsing
When the API interface layer receives the call request, it will receive the ID of the incoming SQL and the incoming object (can be Map/JavaBean or basic data type), and Mybatis will find the corresponding one based on the ID of the SQL. MappedStatement, and then parsed according to the incoming parameter object MappedStatement. After parsing, the SQL statement and parameters to be ultimately executed can be obtained.
SQL execution
Get the final SQL and parameters into the database for execution, and get the results of operating the database.
Result Mapping
Convert the results of operating the database according to the mapping configuration, which can be converted into HashMap/JavaBean or basic data types, and return the final result.
The above is the detailed content of What is the principle and process of ssm framework?. For more information, please follow other related articles on the PHP Chinese website!