Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle and process of ssm framework

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about what is the principle and process of ssm framework. Many people may not understand it very well. In order to make you understand better, the editor summarizes the following content for you. I hope you can get something according to this article.

The principle of ssm framework is to integrate Spring, SpringMVC and MyBatis together as the framework of web projects with simple data sources. The process is as follows: Handler Mapping finds the corresponding Handler according to the request, calls the business logic to process it, and then parses the view.

Brief introduction of SSM Framework

The SSM (Spring+SpringMVC+MyBatis) framework set is formed by the integration of two open source frameworks, Spring and MyBatis (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, and its core Servlet, namely DispatcherServlet, assumes the responsibility of mediation. Matching the user request with Controller,Controller through HandlerMapping is the specific operation performed in response to the request.

SpringMVC framework running process

The user sends the request to DispatcherServlet

DispatcherServlet receives a request to query one or more HandlerMapping to find the Handler that processed the request

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 ModelAndView after processing.

HandlerAdapter returns the Handler processing result ModelAndView to DispatcherServlet

DispatcherServlet queries one or more ViewReslover (view parsers) to pass the ModelAndView to the specified ViewReslover

After parsing, ViewReslover returns the specific View to DispatcherServlet.

DispatcherServlet renders View (populates the model data into the view)

DispatcherServlet responds to the user, and View displays the results on the client.

The process is roughly as follows:

DispatcherServlet is the core of the whole Spring MVC. It is responsible for receiving HTTP requests, organizing and coordinating various components of Spring MVC. Its main work is as follows:

a. Intercept URL requests in accordance with 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. Initialize the components of Spring MVC and install them into DispatcherServlet.

Spring

Spring is like a large factory for assembling bean throughout the project. In the configuration file, you can specify that specific parameters are used to call the constructor of the entity class to instantiate the object. It can also be called the adhesive in the project.

The core idea of Spring is IOC (inversion of Control), which means that programmers are no longer required to explicitly new an object, but rather to let the Spring framework do it for you. The IOC container is responsible for instantiating, locating, configuring objects in the application, and establishing dependencies between those objects. The purpose of Spring is to make the relationship between objects (modules and modules) not associated by code, but managed by configuration class descriptions (Spring dynamically assembles objects internally through reflection according to these configurations).

The IOC container represents the BeanFactory interface in the org.springframework.beans package, which provides the basic functions of the IOC container, while the ApplicationContext interface under the org.springframework.context package extends BeanFactory, and also provides integration with Spring AOP, international handling, event propagation and different levels of context implementation.

In a nutshell, BeanFactory provides the most basic functionality of the IOC container, while ApplicationContext adds more support for enterprise-level functionality. ApplicationContext completely inherits BeanFactory, so the semantics of BeanFactory are also applicable to ApplicationContext.

Spring running process

Load the configuration file, ApplicationContext interface. The information of the bean in the configuration file is loaded in the HashMap, a bean usually includes, id,class,property, etc., the id of the bean corresponds to the value in the key,HashMap in the HashMap is bean.

Call the getBean method. GetBean is used to get the bean in the applicationContext.xml file, and the parameter is the id of bean. In general, it will be forced to the corresponding business layer (interface).

Invokes the method of the business layer (interface implementation).

How is the content in bean injected into it? To put it simply, when you instantiate a bean, you actually instantiate the class, which injects the class attributes previously stored in the HashMap into the class through reflection calling the set method in the class. This brings us back to the original place of our Java, the object. Property, object. Method.

Mybatis

Mybatis is the encapsulation of jdbc, which makes the underlying operation of the database transparent. The operation of Mybatis revolves around an instance of sqlSessionFactory. Mybatis is associated to the Mapper file of each entity class through a configuration file, and the Mapper file configures the sql statement mapping required by each class to the database. Each 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, when executing the method in the dao written by itself, it is actually the corresponding MapperProxy in the proxy.

Operation principle of Mybatis

Load configuration file

The configuration information of SQL is loaded into MappedStatement objects, including parameter mapping configuration, SQL statements executed, and result mapping configuration, which are stored in memory.

SQL parsing

When the API interface layer receives the call request, it will receive the ID and the incoming object (which can be Map/JavaBean or basic data type) of the incoming SQL. Mybatis will find the corresponding MappedStatement according to the ID of the SQL, and then parse it according to the passed parameter object MappedStatement. After parsing, you can get the final SQL statement and parameters to be executed.

SQL execution

The final SQL and parameters are taken to the database for execution, and the results of operating the database are obtained.

Result mapping

The results of manipulating the database are converted according to the mapped configuration, which can be converted to HashMap/JavaBean or basic data types, and the final result is returned.

After reading the above, do you have any further understanding of what is the principle and process of the ssm framework? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report