Introduction to SSM

Introduction

SSM(Spring、SpringMVC和Mybatis)

Spring

Spring is an open source framework
pring born to simplify enterprise-level development. Using Spring development, Bean objects, Dao component objects, Service component objects, etc. can be handed over to the Spring container for management, which makes a lot of complex code developed in Spring. It is very elegant and concise, effectively reducing the coupling of the code, and greatly facilitating the later maintenance, upgrading and expansion of the project.
Spring is an IOC (DI) and AOP container framework.

Springmvc

Springmvc is a web framework based on mvc, it is a module of spring framework, the two do not need to be integrated through intermediate integration.

Execution steps of Springmvc framework

The first step: the user initiates a request to the front controller (DispatcherServlet)

Step 2: The front controller requests the processor mapper to find the Handler

Step 3: The processor mapper returns the Handler to the front controller

Step 4: The front controller calls the processor adapter to execute the Handler

The fifth step: the processor adapter to execute the Handler

Step 6: Handler execution completes and returns ModelAndView to the adapter

Step 7: The processor adapter returns ModelAndView to the front controller

ModelAndView is a low-level object of springmvc framework, including Model and View

Step 8: The front controller requests the view resolver to resolve the view, and resolve it into a real view (jsp) according to the logical diagram name

Step 9: The view resolver returns View to the front controller

Step 10: The front controller performs the view rendering, and the view rendering fills the model data (in the ModelAndView object) into the request field

Step 11: The front controller responds to the user with the result

Common annotations of Springmvc framework

@Controller

@Controller is used to mark a class, and the class marked with it is a springmvc Controller object. The distribution processor will scan the method using the annotation, and detect whether the method uses the @RequestMapping annotation.

@RequestMapping

This annotation is an annotation used to process the request address mapping and can be used on classes or methods. Used on a class, it means that all methods in the class that respond to requests use this address as the parent path.

@Resource和@Autowired

@Resource and @Autowired are both used for bean injection. In fact, @Resource is not an annotation of Spring. Its package is javax.annotation.Resource and needs to be imported, but Spring supports injection of this annotation.

Common points:

Both can be written on fields and setter methods. If both are written on the field, then there is no need to write setter methods.

difference:

@Autowired is an annotation provided by spring. @Autowired annotation is to assemble dependent objects according to the type (byType). By default, it requires dependent objects to exist. If a null value is allowed, you can set its required attribute to false.

@Resource is automatically injected according to byName by default. @Resource has two important attributes: name and type. Spring parses the name attribute annotated by @Resource into the name of the bean, while the type attribute parses into the type of the bean. Therefore, if the name attribute is used, the automatic injection strategy of byName is used, and the automatic injection strategy of bytype is used when the type attribute is used. If neither the name nor the type attribute is specified, then the byName automatic injection strategy will be used through the reflection mechanism.

@ModelAttribute和@SessionAttributes

It means: before calling all methods of the Controller, execute this @ModelAttribute method, which can be used in annotations and method parameters. This @ModelAttribute feature can be applied to BaseController. All Controllers inherit BaseController and can be implemented in transfer

AthPathVariable

Used to map the template variables in the request URL to the parameters of the function processing method, that is, take the variables in the uri template as parameters.

@RequestParam

It is mainly used to obtain parameters in the springmvc background control layer, similar to request.getParameter("name"), which has three commonly used parameters: defaultValue=”0”, required=false, value=”isApp”; defaultValue means to set the default value

@ResponseBody

This annotation is used to convert the object returned by the Controller method to the specified format through an appropriate HttpMessageConverter, and then write it to the body data area of ​​the Response object

When to use: The returned data is not a page of html tags, but data in some other format (such as json, xml, etc.);

@Component

It is equivalent to a general annotation. It is used when you don't know which level some categories belong to, but it is not recommended.

@Repository

Used to annotate the dao layer, annotate on the daoImpl class

How does Springmvc set redirection and forwarding?

Forwarding: Add "forward:" in front of the return value to forward the result, such as "forward:user.do?name=method"

Redirection: Add "redirect:" in front of the return value to redirect the return value, such as "redirect:http://www.baidu.com"

Which controller annotations in Springmvc generally use? Are there other annotations that can be substituted?

The @Controller annotation is generally used to indicate that it is the control layer and cannot be replaced by other annotations.

Is Springmvc's controller in singleton mode? If so, what is the problem and how to solve it?

It is a singleton mode, so thread safety is a problem when multi-threaded access. Do not use synchronization, which will affect performance. The solution is to not write fields in the controller.

What is the effect of @RequestMapping annotation on classes?

It is an annotation used to process the request address mapping, which can be used on classes or methods. Used on a class, it means that all methods in the class that respond to requests use this address as the parent path.

If there are many parameters passed in at the front desk, and these parameters are all an object, how to quickly get this object?

Declare this object directly in the method, and springmvc will automatically assign properties to this object.

There is a class in Springmvc that combines view and data together. What is it called? It's called ModelAndView.

When a method returns a special object to AJAX, such as Object, List, etc., what should be done?

To add @ResponseBody annotation.

Sprigmvc exception handling

The exception can be thrown to the Spring framework, and the Spring framework will handle it; we only need to configure a simple exception handler and add a view page to the exception handler.


Advantages of SpringMVC Advantages of SpringMVC

(1) Spring MVC provides a DispatcherServlet, no additional development is required.

(2) The xml-based configuration file used in springMVC can be edited without recompiling the application.

(3) SpringMVC instantiates the controller and constructs the bean based on user input.

(4) SpringMVC can automatically bind user input and convert data types correctly. For example, springMVC can automatically parse strings and set attributes of type float or decimal.

(5) SpringMVC can check user input, if the check fails, it will redirect back to the input form. Input verification is optional and supports encoding methods and declarations. On this point, springMVC has built-in common validators.

(6) springMVC is part of the spring framework. Other capabilities provided by spring can be used.

(7) springMVC supports internationalization and localization, and supports the display of multiple languages ​​according to the user area.

(8) SpringMVC supports a variety of view technologies, the most common JSP technology and other technologies include Velocity and FreeMarker
detailed explanation:

Clear role division: controller (controller), validator (validator), command object (command obect), form object (form object), model object (model object), Servlet distributor (DispatcherServlet), processor mapping (handler) mapping), view resoler, etc. Each role can be realized by a specialized object.
Powerful and direct configuration method: Both the framework class and the application class can be configured as JavaBeans to support references across multiple contexts, for example, references to business objects and validators in web controllers.
Adaptable and non-intrusive: According to different application scenarios, you can choose the controller subclass (simple type, command type, from type, wizard type, multi-action type or custom) instead of a single controller ( Such as Action/ActionForm) inheritance.
Reusable business code: You can use existing business objects as command or form objects without the need to extend the base class of a specific framework.
Customizable binding and validation: For example, the type mismatch is regarded as an application-level validation error, which can guarantee the wrong value. Another example is the localized date and number binding and so on. In some other frameworks, you can only use string form objects, which need to be parsed manually and converted to business objects.
Customizable handler mapping and view resolution: Spring provides from the simplest URL mapping to complex and dedicated customized strategies. Compared with some web MVC frameworks that force developers to use a single specific technology, Spring is more flexible.
Flexible model conversion: In the Springweb framework, Map-based key/value pairs are used to achieve easy integration with various view technologies.
Customizable localization and theme analysis: support for optional use of Spring tag libraries in JSP, support for JSTL, support for Velocity (no additional middle layer required), etc.
Simple and powerful JSP tag library (Spring Tag Library): support includes many functions such as data binding and theme (theme). He provides maximum flexibility in marking.
JSP form tag library: The form tag library introduced in Spring 2.0 makes it easier to write forms in JSP.
The life cycle of Spring Bean can be limited to the current HTTp Request or HTTp Session. To be precise, this is not a feature of the Spring MVC framework itself, but should belong to the WebApplicationContext container used by Spring MVC.

SSM specifically analyzes
the advantages of the three frameworks of SSM.

1. The advantages and disadvantages of mybatis:

Advantages:
a. SQL is written in the xml file, which facilitates unified management and optimization, and releases the coupling between SQL and program code.

b. Provide mapping tags, support mapping between objects and database orm fields, support object relationship mapping tags, and support the formation of object relationships

c. Provide xml tags to support writing dynamic sql.

Disadvantages:
a. The workload is large, especially when there are many fields in the table and many related tables

b. The writing of sql statements depends on the database, and the portability is poor.

c. Does not support cascading delete, cascading update, you need to delete the table yourself.

Second, the advantages of spring:

A. Through Spring's IOC feature, the dependencies between objects are handed over to Spring to control, which facilitates decoupling and simplifies development.

B. Through Spring's AOP feature, it is easy to control transactions, logs, and permissions.

C. Provides integrated support for other excellent open source frameworks.

D. Low invasiveness.

Three, the advantages of SpringMVC

A. SpringMVC is a lightweight web framework that uses the MVC design philosophy to decouple the web layer, so our development is more concise.

B. Seamless connection with Spring.

C. Flexible data verification, formatting, and data binding mechanism.

Four, integrated analysis

A. Spring MVC + spring +mybatis is a standard MVC design pattern, which divides the entire system into four layers: display layer, controller layer, service layer, and DAO layer

Use Spring MVC to be responsible for request forwarding and view management

Spring implements business object management, and mybatis is used as a persistence engine for data objects.

B. Spring is an open source framework. Spring is a lightweight inversion of control (IoC) and aspect-oriented (AOP) container framework, which can better integrate other frameworks.

C. The Spring MVC framework has an MVC framework that separates data, business and presentation well by implementing the Model-View-Controller pattern.

D. MyBatis is a Java-based persistence layer framework

Guess you like

Origin blog.csdn.net/weixin_46895251/article/details/108479774