Operation mechanism of SSM framework

Time:2022-5-25
My previous article introduced how to build SSH framework and how to use this framework for web application development. Recently, I came into contact with the integration of SSM framework, namely Spring + spring MVC + mybatis. At present, this framework is widely used in terms of SSH framework, but many friends may not know how to use SSM framework, Today, I will teach you how to build an SSM framework and use this framework for a simple application - web login. I hope this example can give you a general understanding of this framework.

Construction of SSM framework

  1. First, download the corresponding jar package. There are two ways for jar packages:

    【1】 Create dependencies using Maven to import the required jar packages.
    【2】 Directly find a blank project on the Internet and find the jar package.
    Import the jar package into the Lib package in Web inf, where I provide the required jar package download. Jar package download address: link: http://pan.baidu.com/s/1i4K8gIh Password: hv7w

  1. Create the required project package
    Because the SSM framework is also designed based on MVC, you generally need to create a package for using the SSM framework
    【1】 Control — package the package where the control layer file is located
    【2】 Entity — the package of the entity class involved in the package project
    【3】 Mapper – package entity maps the package where the file is located. You can create corresponding tables in the database according to the files in it
    【4】 Service — package the package where the service layer interface file is located
    【5】service_ Impl service class implementation class
    【6】 Package of config configuration file
  1. create profile
    SSM framework generally requires the following configuration files:
    【1】applicationContext. XML spring configuration file
    【2】mybatis-config. Configuration file for implementing POJO with XML
    【3】Spring-mvc. XML is a configuration file to realize page logical jump, which is equivalent to struts xml
  1. On the web The XML configuration file configures spring as follows:
<?xml version="1.0" encoding="UTF-8"?>
 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <welcome-file-list>
    <welcome-file>hello.jsp</welcome-file>
  </welcome-file-list>
    <!--  Load spring container configuration -- >
    <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--  Set the path for spring container to load all configuration files -- >
    <context-param>
        <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:config/applicationContext.xml</param-value>
    </context-param>
    <!--  Configure spring MVC core controller -- >
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:config/spring-mvc.xml</param-value>
        </init-param>
        <!--  Start loading once -- >  
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Mapping dispatcherservlet -- >
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <!--  It can be configured as * do -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!--  Prevent spring memory overflow listener -- >
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <!--  Solve engineering coding filter -- >
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

SSM framework application – realize simple login

Let me explain the operation process of SSM framework by logging in as a simple example:

【1】 First step

Load web XML, which tells the program to load ApplicationContext XML and spring MVC xml

【2】 Step two

Enter ApplicationContext XML configuration file, the first is the basic information of the database used to load the project

After that, the configuration file mybatis config. Will be scanned XML configuration file

Automatically create corresponding tables in the database according to the object relationship mapping file

【3】 Step 3

According to web The XML file first enters hello. XML jsp
After entering, the program is handed over to the control class management in the control package
The findByName method in the service layer will be called in the control class
Then the program is handed over to the service management layer
The service then calls the findByName method in the Dao layer
The program is then handed over to the Dao layer for management
Dao layer generally calls some methods of mybatis to add, delete, modify and check

【4】 After the above process is completed

The results returned from Dao layer processing will be handed over to the controller for processing, and the classes in the controller will jump to the corresponding page according to the edited logic program.

summary

Spring MVC: it mainly accepts and responds to the request of the page.
The components include: front-end controller, processor mapper, processor adapter, view parser, processor handler and view. Among them, only processor handler and view need to be developed by programmers.
View is an interface. The implementation class supports different view types (JSP, freemaker, PDF, etc.). In addition, the processor handler is the controller.

Service layer function: inject Dao and call Dao method
Mybatis: when you start mybatis, you should remember the ultimate goal. We need to operate the data in the database, so you just need to remember that it is very convenient, very convenient, convenient. (it will take a whole day to explain myabtis configuration in detail, so I won’t repeat it)

So, finally, our development idea is very clear. Get the data from the page to the controller layer, then transfer it to the service layer for relevant operations (calling methods, etc.), and then transfer it to the persistence layer to add, delete, modify and check the database accordingly. Is that a lot clearer?

Source source download address:

Link: http://pan.baidu.com/s/1o87MSMQ Password: whf1