Spring SpringMvc Mybatis 整合即SSM整合

前言

整合SSM是学习Spring、SpringMvc和Mybatis更加深层的理解,本篇也仅列举常用配置,仅供初学者配置和自己日后查阅。本篇采用idea开发结合maven使得项目配置更加简单。

本文默认读者会maven项目创建,已省略idea中maven创建流程,若需要后续出教程

1. maven中pom.xml文件配置

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>

    <!--  maven项目名称,请根据自身项目修改-->
    <name>tireless</name>
    <groupId>cn.tireless</groupId>
    <artifactId>tireless</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!--    设置maven打包编码-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--    打包和编译环境jdk1.8-->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- SpringMVC核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!-- Spring JDBC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!-- aspects框架包已包含 Spring AOP -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!-- MyBatis依赖包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.2</version>
        </dependency>

        <!-- MyBatis整合Spring的适配包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- 这里采用C3P0数据源可更换其他数据源,其他数据源请修改配置文件 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!-- ServletAPI -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!--  junit单元测试包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--  单元测试注解包-->
        <dependency>

            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!--  slf4j日志包-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>

        <!-- MySQL 5版本驱动,若使用mysql8.0需修改 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
        </plugins>
        <finalName>tireless</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.xml</include>
                    <include>*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/*.html</include>
                </includes>
            </resource>
        </resources>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2. Spring配置文件applicationContext.xml配置(ps:建议命名为applicationContext.xml)

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 加载外部文件 这里加载的是数据库配置文件-->
  <context:property-placeholder location="classpath:dbconfig.properties"/>

  <!-- 开启IOC扫描 这里是根据自身项目扫描,仅扫描service包下 -->
  <context:component-scan base-package="com.ssm.service"/>

  <!-- 配置 C3P0 数据源 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  >
    <property name="user" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="driverClass" value="${jdbc.driverClass}"/>
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
    <property name="initialPoolSize" value="${jdbc.initialSize}"/>
    <property name="maxPoolSize" value="10"/>
  </bean>
  <!-- 配置MyBatis SqlSessionFactory -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 指定MyBatis 数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 指定MyBatis mapper文件的位置 -->
    <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"/>
    <!-- 指定MyBatis全局配置文件的位置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
  </bean>

  <!--扫描MyBatis的mapper接口 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--扫描所有dao接口的实现,加入到ioc容器中 -->
    <property name="basePackage" value="cn.xyes.mapper"/>
  </bean>

  <!-- 开启Spring事务 -->
  <bean id="txManager"    
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>

  <!-- 开启事务注解 -->
  <tx:annotation-driven transaction-manager="txManager" />

</beans>

3.SpringMVC配置文件springmvc.xml

<?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"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <!-- 告知spring,启用springmvc的注解驱动 annotation: 注解-->
        <mvc:annotation-driven />
    <!--component  : 组件-->
    <!-- 扫描Controller包下类 -->
    <context:component-scan
            base-package="com.ssm.controller">
    </context:component-scan>
    <!-- 注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 配置视图解析器 -->
  <bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/"/>
      <property name="suffix" value=".html"/>
  </bean>
    <mvc:view-controller path="/" />
    <mvc:default-servlet-handler/>

</beans>

3.Mybatis配置文件,这里大部分已经配置到spring的配置文件中,因此配置内容比较少

<?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>
	<settings>
		<!-- 打印SQL-->
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>
	
	<typeAliases>
	<!-- 指定一个包名,MyBatis会在包名下搜索需要的JavaBean(即dao层包)-->
	<package name="com.ssm.mapper"/>
    </typeAliases>

</configuration>

4.数据库配置文件dbconfig.properties配置

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=20
jdbc.initialSize=5

5.web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


  <!-- 加载Spring -->
  <context-param>
    <param-name>
      contextConfigLocation
    </param-name>
    <param-value>
      classpath:applicationContext.xml
    </param-value>
  </context-param>
  <!-- Listener监听  -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <!-- 加载静态资源 -->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>


  <!--springmvc前端核心控制器-->
  <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:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- 字符编码过滤器,一定要放在所有过滤器之前 -->
  <!-- 字符编码过滤器 -->
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>  

5.SSM项目目录预览

总结

本篇SSM整合配置目的是能让学过但未整合过的同学复制粘贴后即可食用,因为博主刚开始整合是也走过不少弯路,配置文件东凑西凑就是跑不起来,所以记录一下,实际中ssm使用并不多(现在大多使用springboot)ssm仅为初学者必经学习的项目,博主也有整合redis和jwt配置文件,和一些整合遇到的坑,如果需要帮助可以留言或者私信我。因水平有限若有不合理指出请评论指出,谢谢。

posted @ 2022-02-13 20:36  Tireless  阅读(57)  评论(0编辑  收藏  举报