SSM three major framework series: Spring 5 + Spring MVC 5 + MyBatis 3.5 integration (with source code)

quote

Before finishing the new version of the three major SSM framework, 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, MyBatis 3.5.2.

Background introduction

ssm-demo is the first open source project I posted on GitHub. The project should be developed at the end of 2016, and then I chose to open source this project to the GitHub open source platform.

ssm-demo

It was a relatively common phenomenon to use Spring 4.X version when developing Java Web projects at that time in 2016, and it does not make people feel that the version is behind, but it is already 2020, so Spring was carried out on the basis of the original code. Relevant framework upgrades, so I organized the integration of the three major SSM framework tutorials related to this Spring 5 version.

Spring 5 is here

Spring 5 is a very important version update. You can also see that this version is updated frequently in its open source warehouse. Everyone should believe that this version will gradually take up an increasing share in enterprise development.

Briefly talk about the version number selected in this tutorial:

  1. The Spring version chose a relatively new version, which 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 version update log was v5.2.2.RELEASE .

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

  3. The version of MyBatis is upgraded to 3.5.2, MyBatis 3.5 and above corresponds to Spring 5 and above, Spring 4 does not support MyBatis 3.5 and above, the corresponding version of mybatis-spring also needs to be upgraded to version 2.0 and above, I chose to 2.0.1.

The above is the version selection of the three major 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 errors.

Main code integration

The directory structure of the source code is as follows:

pom dependency

The main version numbers of the three major frameworks of Spring Spring MVC MyBatis have been introduced, here I directly give the pom.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-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依赖 -->
        <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依赖 -->

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

        <!-- 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依赖 -->

        <!-- Begin: 数据库依赖包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${jdbc.driver.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!-- End: 数据库依赖包 -->

        <!-- Begin: aspectj相关jar包-->
        <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包-->

        <!-- Begin: Servlet相关依赖包 -->
        <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>

    <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>
复制代码

Just wait patiently for the jar package to download.

Spring configuration file

The applicationContext.xml configuration file is as follows:

  • Turn on packet scanning
  • Configure the 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">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.ssm.demo.dao"/>
    <context:component-scan base-package="com.ssm.demo.service"/>

    <!-- 配置数据源 -->
    <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"/>
        <!-- 改为你的地址即可 -->
        <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"/>
        <!-- 自动扫描mappers.xml文件 -->
        <!--<property name="mapperLocations" value="classpath:mappers/*.xml"></property>-->
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <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>

    <!-- 配置事务通知属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定义事务传播属性 -->
        <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>

    <!-- 配置事务切面 -->
    <aop:config>
        <aop:pointcut id="serviceOperation"
                      expression="(execution(* com.ssm.demo.service.*.*(..)))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>

</beans>
复制代码

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">

    <!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.ssm.demo.controller"/>

    <!-- 视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
复制代码

Enable Controller package scanning and view parser configuration.

MyBatis configuration

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.ssm.demo.entity"/>
    </typeAliases>
</configuration>
复制代码

web.xml configuration

Finally, the most important configuration file in the Java Web project: web.xml. Here we will load the relevant configuration of Spring and start the Spring container. At the same time, we will configure SpringMVC and hand over all requests to the front controller DispatcherServlet for processing.

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-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!--End 欢迎页-->

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

    <!--Start 编码过滤器 解决乱码问题-->
    <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 编码过滤器 解决乱码问题-->

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

    <!--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>
复制代码

JSP page

Finally, there is a JSP page for testing. Create a new index.jsp in the webapp directory. The code is as follows:

<%@ 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>
复制代码

Project launch and preview

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

The integration of the three major SSM frameworks of Spring 5 is successful!

This is an integration tutorial for Spring 5 + Spring MVC 5 + MyBatis 3.5. It is just for integration. It is for everyone to refer to when upgrading Spring 5. Other functions can be implemented by themselves. It can also be expanded with the open source project I wrote before.

Precautions

  • Request suffix

What is set here is to intercept the request at the end of .do, you can modify it according to your needs.

  • Mapper file

Since there is no Mapper file added to the project, I commented out this line of configuration. After you add the Mapper file, you need to remove this line of comment.

Source code download

The download link is as follows:

download.csdn.net/download/ZH…

During the upload, I set that I do n’t need to download points. If I need to download points later, it should be set by the CSDN platform.

Write at the end

As a small promotion, interested friends can take a look. Recently, I published a small booklet "Spring Boot Large-scale Online Mall Project Practical Tutorial" on the Nuggets platform (click the link or click the picture below to purchase to get a discount 8 Fold ):

my-xiaoce

The booklet will focus on the Spring Boot technology stack, and other technical frameworks used will also take into account the latest technology trends and expand knowledge, from shallow to deep, step by step, while learning the basics, you can also master certain development skills, not just learning Spring. Boot's fur is also aware of its source code design and internal principles. It not only learns the integration of Spring Boot's related technology stacks, but also can use the Spring Boot technology stack to build a large shopping mall system, so that you have a high-quality learning progress. Level experience. Staying away from the Hello World project, you can not only get a complete hands-on project, but also help you fill the current hot Spring Boot technology stack, providing sufficient protection for your technical depth and salary promotion.

This is a practical project of a shopping mall. Some page previews are as follows:

  • Home

    index-1

  • Order List

    my-orders

Interested friends can look at.

Except for indicating the reprint / source, the authors are all original and welcome to reprint, but this statement must be retained without the author ’s consent, and the original text link is given in an obvious position on the article page, otherwise the right to pursue legal responsibility is reserved.

Thank you for watching. I am thirteen. The article was first published on my public account "The Little Story of a Programmer".

Guess you like

Origin juejin.im/post/5e5a722c6fb9a07ccb7e9a32