This article is about its integration process and project source code. The version numbers are: Spring 5.2.2.RELEASE, SpringMVC 5.2.2.RELEASE and MyBatis 3.5.2.

background

Ssm-demo was the first open source project I published on GitHub at the end of 2016. After that, I chose to open source the project on GitHub.

It was common to use Spring 4.x for Java Web project development at that point in 2016, and it didn’t feel like the release was behind, but it was 2020, so we upgraded the Spring-related framework based on the original code. Therefore, I have put together the SSM three framework integration tutorial for this Spring 5 release.

Spring may come

Spring 5 is a very important release update, and you can see it in the open source repository very frequently, and you can expect it to take up a larger and larger share of enterprise development over time.

A brief description of the version number chosen for this tutorial:

  1. The Spring version was upgraded from the original 4.2.4.RELEASE to 5.2.2.RELEASE. This version was officially released on October 3, 2019, and the update log was V5.2.2.release.

  2. The Spring MVC version is also updated with the Spring RELEASE, also 5.2.2.release.

  3. MyBatis 3.5 or later corresponds to Spring 5 or later. Spring 4 does not support MyBatis 3.5 or later. The corresponding version of Mybatis – Spring also needs to be upgraded to a version above 2.0. I chose 2.0.1.

The above is the version selection of the three SSM frameworks. The version number is selected and integrated by myself. You can fine-tune it, but you must pay attention to the test and do not report any errors.

Major code integration

The directory structure of the source code is as follows:

Pom depends on

The main version numbers of the three frameworks of Spring SpringMVC MyBatis have been introduced. Here I directly give the POP.xml file:

<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ssm.demo</groupId>
    <artifactId>ssm-demo</artifactId>
    <packaging>war</packaging>
    <version>1.0 the SNAPSHOT</version>
    <name>ssm-demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring.version>5.2.2. RELEASE</spring.version>
        <java.version>1.8</java.version>
        <jdbc.driver.version>8.0.16</jdbc.driver.version>
        <aspectj.version>1.9.5</aspectj.version>
        <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
        <jsp-api.version>2.2</jsp-api.version>
        <jstl.version>1.2</jstl.version>
        <mybatis.version>3.5.2</mybatis.version>
        <mybatis-spring.version>2.0.1</mybatis-spring.version>
        <maven.test.skip>true</maven.test.skip>
    </properties>

    <dependencies>
        <! -- Begin: Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <! -- End: Spring dependency -->

        <! -- Begin: SpringMVC dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <! -- End: SpringMVC dependency -->

        <! -- Begin: mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>
        <! -- End: mybatis dependency -->

        <! -- Begin: database dependency package -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${jdbc.driver.version}</version>
            <scope>runtime</scope>
        </dependency>
        <! -- End: database dependency package -->

        <! -- Begin: AspectJ related JAR package -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <! -- End: AspectJ jar package -->

        <! -- Begin: Dependencies for servlets -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <! -- End: Servlet dependencies -->
    </dependencies>

    <build>
        <finalName>ssm-demo</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>
Copy the code

Wait patiently for the JAR package to download.

Spring configuration file

The applicationContext.xml configuration file is as follows:

  • Enabling Packet Scanning
  • Configuring a Data Source
  • MyBatis configuration
  • Transaction Management configuration
  • Transaction aspect configuration
<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <! -- Automatic scan -->
    <context:component-scan base-package="com.ssm.demo.dao"/>
    <context:component-scan base-package="com.ssm.demo.service"/>

    <! -- Configure data source -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/ssm-demo"/>
        <! -- Change it to your address.
        <property name="username" value="root"/>
        <property name="password" value="131313"/>
    </bean>


    <! Mybatis sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <! -- Automatically scan mappers. XML -->
        <! --<property name="mapperLocations" value="classpath:mappers/*.xml"></property>-->
        <! -- Mybatis config file -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <! Spring will automatically find the class under the package name of the DAO interface.
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.demo.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <! -- transaction Manager, use JtaTransactionManager for Global TX -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <! -- Configure transaction notification properties -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <! Define transaction propagation properties -->
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="upd*" propagation="REQUIRED"/>
            <tx:method name="edit*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="new*" propagation="REQUIRED"/>
            <tx:method name="set*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="change*" propagation="REQUIRED"/>
            <tx:method name="check*" propagation="REQUIRED"/>
            <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="search*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="load*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <! -- Configure transaction facets -->
    <aop:config>
        <aop:pointcut id="serviceOperation"
                      expression="(execution(* com.ssm.demo.service.*.*(..) ))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>

</beans>
Copy the code

For SpringMVC configuration

The spring-mvC.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <! -- Packages using annotations, including subsets -->
    <context:component-scan base-package="com.ssm.demo.controller"/>

    <! -- View resolver -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
Copy the code

Enable Controller package scanning and view resolver configuration.

MyBatis configuration

The mybatis-config.xml configuration file is as follows:

<?xml version="1.0" encoding="UTF-8" ? >

      
<configuration>
    <typeAliases>
        <package name="com.ssm.demo.entity"/>
    </typeAliases>
</configuration>
Copy the code

The web.xml configuration

Finally, the most important configuration file in a Java Web project is web.xml, where we load the Spring configuration and start the Spring container, configure SpringMVC, and hand all requests to the front-end controller DispatcherServlet to handle.

The web.xml configuration file is as follows:

<?xml version="1.0" encoding="UTF-8"? >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">
    <display-name>ssm-demo</display-name>
    
    <! Start welcome page -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <! -- welcome page -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <! --Start filter to solve garble problem -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/ *</url-pattern>
    </filter-mapping>
    <! --End code filter to solve the problem of garbled characters -->

    <! --Start spring listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <! --End Start spring listener -->

    <! --Start spring mvc servlet-->
    <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:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <! --End spring mvc servlet-->

    <! --Start servlet-mapping -->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <! --End servlet-mapping -->

</web-app>
Copy the code

The JSP page

Finally, a JSP page is created for testing. Create a new index.jsp in the WebApp directory with the following code:

<% @ page contentType="text/html; charset=UTF-8" language="java"% >
<html>
<head>
    <title>ssm demo</title>
</head>
<body>Hello SSM (Spring 5.2 + MyBatis 3.5)!</body>
</html>
Copy the code

Project launch and preview

I have tested the source code. After deploying it to Tomcat and starting it, you can see the following page:

Spring 5 SSM three framework integration success!

This is a Spring 5 + Spring MVC 5 + MyBatis 3.5 integration tutorial, just integration, for everyone in the upgrade of Spring 5 do reference, other features self-implementation, can also be combined with the open source project I wrote before the expansion.

Matters needing attention

  • Request the suffix

This is set up to block requests ending in.do, which you can modify as needed.

  • Mapper file

Since Mapper file was not added to the project, I commented out this line of configuration. You will need to comment out this line after adding Mapper file later.

Download the source code

Download link below:

Download.csdn.net/download/ZH…

I set that no credits need to be downloaded when uploading. If credits need to be downloaded later, it should be set by CSDN platform.

Write in the last

For a small promotion, interested friends can have a look, RECENTLY I published a small book on the Gold digging platform “Spring Boot Large-scale online mall Project Practical Tutorial” (click the link or click on the picture below to buy 20% off oh) :

Small volume will be around the Spring Boot technology stack, the use of other technical framework will also take into account the latest technology trends, to expand the knowledge, from shallow to deep, step by step, in the study of the foundation at the same time can also master certain development skills, not only learn Spring Boot fur, but also know its source code design and internal principles, Not only learning the integration of Spring Boot related technology stack, but also using Spring Boot technology stack to build a large mall system, so that you can have a high-quality learning experience. Stay away from the Hello World project, so that you can not only get a complete hands-on project, but also help you click on the current hot Spring Boot technology stack, for your technical depth and salary promotion to provide sufficient guarantee.

This is a mall actual combat project, part of the page preview is as follows:

Interested friends can pay attention to it.

In addition to indicate the reprint/source, all for the author’s original, welcome to reprint, but without the consent of the author must retain this statement, and give the original link in the obvious position of the article page, otherwise reserve the right to pursue legal responsibility.

Thank you for watching, I am 13, the article first appeared in my public account “programmer’s Short story”.