SSM Framework principle function and usage method

Source: Internet
Author: User
Tags aop exception handling

function :

The SSM framework is the integration of spring MVC, Spring and mybatis frameworks, which are standard MVC patterns that divide the entire system into presentation layers, controller layers, service layers, and DAO layers four layers

Use spring MVC for forwarding and view management of requests

Spring implements business object management, MyBatis as a persistence engine for data Objects

principle :

SPRINGMVC:

1. Client sends request to Dispacherservlet (dispatcher)

2. Query the handermapping by the Dispacherservlet controller to find the controller that handles the request

3.Controller returns Modelandview after calling business logic processing

4.DispacherSerclet Query view parser, find Modelandview specified view

5. The view is responsible for displaying the results to the client


Spring: We usually develop the most contact estimate is the IOC container, it can load beans (that is, our class in Java, of course, including service DAO), with this mechanism, we do not have to use this class to initialize it every time, You rarely see the keyword new. In addition, spring's AOP, transaction management and so on are often used by us.

Mybatis: Mybatis is the encapsulation of JDBC, which makes the underlying operation of the database transparent. The operation of MyBatis is carried out around a sqlsessionfactory instance. MyBatis is associated to the mapper file of each entity class through a configuration file, the SQL statement mappings required by each class to the database are configured in the mapper file. Each time you interact with the database, you get a sqlsession by Sqlsessionfactory and then execute the SQL command.


How to use :

To complete a feature:

First, the realist entity, which defines the properties of the object, can be set according to the fields of the table in the database, and the database should be designed before all encodings begin.

Write Mapper.xml (Mybatis), which defines your function, corresponding to the actions you want to make to the database, such as INSERT, SelectAll, Selectbykey, delete, update, and so on.

Write Mapper.java to map the operations in Mapper.xml to Java functions by ID.

Write Service.java, provide services for the control layer, accept the parameters of the control layer, complete the corresponding functions, and return to the control layer.

Write Controller.java, connect the page request and service layer, get the parameters of the page request, through the automatic assembly, map the different URLs to the corresponding processing functions, and get the parameters, the parameters are processed, and then passed to the service layer.

Write JSP page call, request which parameters, need to get what data.

DataBase ===> Entity ===> mapper.xml ===> mapper.java ===> service.java ===> controller.java ===> Jsp.

Spring MVC has a controller that acts like struts, receiving external requests and parsing parameters to the service layer
The Spring container is a coordination context that manages dependencies between objects and provides a transactional mechanism
MyBatis belongs to the ORM Persistence layer Framework, which unites business entities with data tables.


Spring MVC control layer, want to be the role of struts
Spring control inversion and dependency injection creation objects are managed by the container to realize the role of decoupling
MyBatis mainly used to operate the database (database additions and deletions)


IOC: Inversion of control is a design idea to reduce the coupling between objects, and it is better to give an example when interviewing, to deepen understanding. Example: Rent a house, used to rent a house need a house to find a house, time-consuming and laborious, and then now join a housing intermediary, the room type you need to tell the intermediary, you can directly select the required house, intermediary is equivalent to the spring container.


AOP: Aspect-oriented programming is a complement to object-oriented development that allows developers to dynamically modify models to meet new requirements without altering the original model, such as dynamically increasing logs, security, or exception handling. AOP reduces the coupling between parts of business logic, improves the reusability of programs and improves development efficiency.

Persistence layer : DAO layer (Mapper)

DAO Layer: DAO layer is mainly to do the work of data persistence layer, some tasks that are responsible for contacting with database are encapsulated here,

The DAO layer is designed primarily to design the DAO interface,

The implementation class for this interface is then defined in the spring configuration file.

This interface can then be called in the module to handle the data business, without concern for the class of the specific implementation class of this interface, it is very clear structure,

The data source configuration for the DAO layer, as well as the parameters for database connections, are configured in spring's configuration file. Business Layer : Service layer

Service layer: The service layer is responsible for the logic application design of the business module.

First design the interface, and then design its implementation of the class

The Association of its implementation is then configured in the spring configuration file. This allows us to invoke the service interface in the application for business processing.

Service layer of the business implementation, specifically to call to the defined DAO layer interface,

The business logic that encapsulates the service layer facilitates the independence and reuse of common business logic, and the program appears very concise. Presentation Layer : Controller layer (handler layer)

Controller layer: Controller layer is responsible for the specific business module Process Control ,

In this layer to invoke the service layer interface to control the business process,

The configuration of the control is also in the spring configuration file, for the specific business process, there will be different controllers, our specific design process can be abstracted, design a reusable sub-unit process module, so that not only the program structure becomes clear, but also greatly reduce the amount of code. View Layer

This layer of view layer is closely related to the control layer and needs to be combined to work together. The view layer is primarily responsible for the presentation of the foreground JSP page. all levels of contact

DAO layer, service layer these two levels can be developed independently, the coupling degree is very low, completely independent, such a pattern in the development of large projects in the process of particular advantages

Because of the high coupling degree, the Controller,view layer should be developed together, but it can be considered as a whole independently of the first two layers. In this way, before the layer and layer we just need to know the definition of the interface, call the interface to complete the required logic unit application, everything seems very clear and simple.

Service Logic Layer Design

The service layer is built on the DAO layer, and the service layer can be established after the DAO layer is established, and the service layer is under the controller layer, so the service layer should call the interface of the DAO layer. It also provides the interface to the controller layer's class to make the call, which is just in the middle of an intermediate position. Each model has a service interface, each of which encapsulates its own business processing methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.