Spring MVC是Spring框架中的一个模块,所以Spring MVC与Spring之间不存在整合的问题,只要引入相应JAR包就可以直接使用。因此SSM框架的整合就只涉及到了Spring与MyBatis的整合,以及Spring MVC与MyBatis的整合。Spring与MyBatis框架的整合时,通过Spring实例化Bean,然后调用实例对象中的查询方法来执行MyBatis映射文件中的SQL语句的,如果能够正确查询出数据库中的数据,那么就可以认为Spring与MyBatis框架整合成功。加入Spring MVC后,如果可以通过前台页面来执行查询方法,并且查询出的数据能够在页面中正确显示,那么就可以认为三大框架整合成功。

1、dao层
  1.1、必须数据:
       pojo(java实体)、mapper接口(dao接口)、sql映射文件(翻译为jdbc中的statement)
  1.2、配置文件:
       mybatis核心配置文件:​SqlMapConfig.xml
          自定义别名
       spring整合mybatis配置文件:​applicationContext-dao.xml
         数据源
         会化工厂
         mapper扫描(加载dao)
       其他配置:
         数据库信息:​db.properties
         日志信息:​log4j.properties
2、service层
  2.1、配置文件:
       注解扫描:@service(加载service)​applicationContext-service.xml
       事务配置: ​ApplicationContext-trans.xml
3、controller层
  3.1、配置文件: ​springMVC.xml
       注解扫描:@controller(加载controller)
       注解驱动:自动加载最新的处理器映射器、处理器适配器
       视图解析器:
       自定义类型转化器:(挂载于注解驱动)
       文件上传解析器:
       json格式数据解析器:(若没有配置注解驱动,则需要显示的在处理器适配器挂载)
       全局异常处理器:
       自定义拦截器:
       静态资源放行:
       
4、web.xml web.xml
  4.1、spring监听器
  4.2、springMVC前端控制器
  4.3、post中文乱码请求

SqlMapConfig.xml

<?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>
<properties resource="db.properties"></properties><span ><<span >typeAliases</span>></span>
<span ><!-- 定义单个pojo类别名
type:类的全路劲名称
alias:别名
--></span>
<span ><!-- <typeAlias type="xx.xxxx.pojo.User" alias="user"/> --></span>

<span ><!-- 使用包扫描的方式批量定义别名
定以后别名等于类名,不区分大小写,但是建议按照java命名规则来,首字母小写,以后每个单词的首字母大写
--></span>
<span ><<span >package</span> <span >name</span>=<span >"xx.xxx.pojo"</span>/></span>
<span ></<span >typeAliases</span>></span>

</configuration>

applicationContext-dao.xml

<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><span ><!-- 配置 读取properties文件 jdbc.properties --></span>
<span ><<span >context:property-placeholderlocation="classpath:jdbc.properties"</span>/></span>

<span ><!-- 配置 数据源 --></span>
<span ><<span >beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"</span>></span>
<span ><!-- 驱动 --></span>
<span ><<span >propertyname="driverClassName"value="${jdbc.driver}"</span>/></span>
<span ><!-- url --></span>
<span ><<span >propertyname="url"value="${jdbc.url}"</span>/></span>
<span ><!-- 用户名 --></span>
<span ><<span >propertyname="username"value="${jdbc.username}"</span>/></span>
<span ><!-- 密码 --></span>
<span ><<span >propertyname="password"value="${jdbc.password}"</span>/></span>
<span ></<span >bean</span>></span>

<span ><!-- 配置 Mybatis的工厂 --></span>
<span ><<span >beanclass="org.mybatis.spring.SqlSessionFactoryBean"</span>></span>
<span ><!-- 数据源 --></span>
<span ><<span >propertyname="dataSource"ref="dataSource"</span>/></span>
<span ><!-- 配置Mybatis的核心 配置文件所在位置 --></span>
<span ><<span >propertyname="configLocation"value="classpath:SqlMapConfig.xml"</span>/></span>
<span ><!-- 配置pojo别名 --></span>
<span ><<span >propertyname="typeAliasesPackage"value="xx.xxxx.core.bean"</span>></span><span ></<span >property</span>></span>
<span ></<span >bean</span>></span>

<span ><!-- 配置 1:原始Dao开发 接口实现类 Mapper.xml 三个 2:接口开发 接口 不写实现类 Mapper.xml 二个 (UserDao、ProductDao
、BrandDao。。。。。。。) 3:接口开发、并支持扫描 xx.xxxx.core.dao(UserDao。。。。。) 写在此包下即可被扫描到 --></span>
<span ><<span >beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"</span>></span>
<span ><<span >propertyname="basePackage"value="xx.xxxx.core.dao"</span>/></span>
<span ></<span >bean</span>></span>

</beans>


db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
  • <

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

ApplicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><span ><!-- @Service扫描 --></span>
<span ><<span >context:component-scan</span> <span >base-package</span>=<span >"xx.xxxxx.service"</span>></span><span ></<span >context:component-scan</span>></span>

</beans>

ApplicationContext-trans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><span ><!-- 事务管理器 --></span>
<span ><<span >bean</span> <span >id</span>=<span >"transactionManager"</span>
<span >class</span>=<span >"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span>></span>
<span ><!-- 数据源 --></span>
<span ><<span >property</span> <span >name</span>=<span >"dataSource"</span> <span >ref</span>=<span >"dataSource"</span> /></span>
<span ></<span >bean</span>></span>

<span ><!-- 通知 --></span>
<span ><<span >tx:advice</span> <span >id</span>=<span >"txAdvice"</span> <span >transaction-manager</span>=<span >"transactionManager"</span>></span>
<span ><<span >tx:attributes</span>></span>
<span ><!-- 传播行为 --></span>
<span ><<span >tx:method</span> <span >name</span>=<span >"save*"</span> <span >propagation</span>=<span >"REQUIRED"</span> /></span>
<span ><<span >tx:method</span> <span >name</span>=<span >"insert*"</span> <span >propagation</span>=<span >"REQUIRED"</span> /></span>
<span ><<span >tx:method</span> <span >name</span>=<span >"delete*"</span> <span >propagation</span>=<span >"REQUIRED"</span> /></span>
<span ><<span >tx:method</span> <span >name</span>=<span >"update*"</span> <span >propagation</span>=<span >"REQUIRED"</span> /></span>
<span ><<span >tx:method</span> <span >name</span>=<span >"find*"</span> <span >propagation</span>=<span >"SUPPORTS"</span> <span >read-only</span>=<span >"true"</span> /></span>
<span ><<span >tx:method</span> <span >name</span>=<span >"get*"</span> <span >propagation</span>=<span >"SUPPORTS"</span> <span >read-only</span>=<span >"true"</span> /></span>
<span ></<span >tx:attributes</span>></span>
<span ></<span >tx:advice</span>></span>

<span ><!-- 切面 --></span>
<span ><<span >aop:config</span>></span>
<span ><<span >aop:advisor</span> <span >advice-ref</span>=<span >"txAdvice"</span>
<span >pointcut</span>=<span >"execution(* xx.xxxx.service.*.*(..))"</span> /></span>
<span ></<span >aop:config</span>></span>

</beans>

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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><span ><!-- @Controller注解扫描 --></span>
<span ><<span >context:component-scan</span> <span >base-package</span>=<span >"xx.xxxx.controller"</span>></span><span ></<span >context:component-scan</span>></span>

<span ><!-- 注解驱动:
替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 --></span>
<span ><<span >mvc:annotation-driven</span> <span >conversion-service</span>=<span >"conversionService"</span>></span><span ></<span >mvc:annotation-driven</span>></span>

<span ><!-- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
--></span>
<span ><<span >bean</span> <span >class</span>=<span >"org.springframework.web.servlet.view.InternalResourceViewResolver"</span>></span>
<span ><!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 --></span>
<span ><!-- 前缀 --></span>
<span ><<span >property</span> <span >name</span>=<span >"prefix"</span> <span >value</span>=<span >"/WEB-INF/jsp/"</span>></span><span ></<span >property</span>></span>
<span ><!-- 后缀 --></span>
<span ><<span >property</span> <span >name</span>=<span >"suffix"</span> <span >value</span>=<span >".jsp"</span>></span><span ></<span >property</span>></span>
<span ></<span >bean</span>></span>

<span ><!-- 配置自定义转换器
注意: 一定要将自定义的转换器配置到注解驱动上
--></span>
<span ><!--<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!– 指定自定义转换器的全路径名称 –>
<bean class="xx.xxxx.controller.converter.CustomGlobalStrToDateConverter"/>
</set>
</property>
</bean>--></span>

<span ><!-- 配置全局异常处理器 --></span>
<span ><<span >bean</span> <span >class</span>=<span >"xx.xxxx.exception.CustomGlobalExceptionResolver"</span>></span><span ></<span >bean</span>></span>

<span ><!-- 文件上传 --></span>
<span ><<span >bean</span> <span >id</span>=<span >"multipartResolver"</span>
<span >class</span>=<span >"org.springframework.web.multipart.commons.CommonsMultipartResolver"</span>></span>
<span ><!-- 设置上传文件的最大尺寸为5MB --></span>
<span ><<span >property</span> <span >name</span>=<span >"maxUploadSize"</span>></span>
<span ><<span >value</span>></span>5242880<span ></<span >value</span>></span>
<span ></<span >property</span>></span>
<span ></<span >bean</span>></span>

<span ><!-- 在注解适配器中加入messageConverters --></span>
<span ><!-- 注意:如果使用<mvc:annotation-driven /> 则不用定义下面的内容。--></span>
<span ><!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">--></span>
<span ><!--<property name="messageConverters">--></span>
<span ><!--<list>--></span>
<span ><!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>--></span>
<span ><!--</list>--></span>
<span ><!--</property>--></span>
<span ><!--</bean>--></span>


<span ><!-- 配置拦截器 --></span>
<span ><!--<mvc:interceptors>
<mvc:interceptor>
<!– 拦截请求的路径 要拦截所有必需配置成/** –>
<mvc:mapping path="/**"/>
<!– 指定拦截器的位置 –>
<bean class="xx.xxxx.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>--></span>

</beans>


web.xml

<?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>ssm0523</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list><span ><!-- 加载spring容器 --></span>
<span ><<span >context-param</span>></span>
<span ><<span >param-name</span>></span>contextConfigLocation<span ></<span >param-name</span>></span>
<span ><<span >param-value</span>></span>classpath:ApplicationContext-*.xml<span ></<span >param-value</span>></span>
<span ></<span >context-param</span>></span>
<span ><<span >listener</span>></span>
<span ><<span >listener-class</span>></span>org.springframework.web.context.ContextLoaderListener<span ></<span >listener-class</span>></span>
<span ></<span >listener</span>></span>


<span ><!-- springmvc前端控制器 --></span>
<span ><<span >servlet</span>></span>
<span ><<span >servlet-name</span>></span>springMvc<span ></<span >servlet-name</span>></span>
<span ><<span >servlet-class</span>></span>org.springframework.web.servlet.DispatcherServlet<span ></<span >servlet-class</span>></span>
<span ><<span >init-param</span>></span>
<span ><<span >param-name</span>></span>contextConfigLocation<span ></<span >param-name</span>></span>
<span ><<span >param-value</span>></span>classpath:SpringMvc.xml<span ></<span >param-value</span>></span>
<span ></<span >init-param</span>></span>
<span ><!-- 在tomcat启动的时候就加载这个servlet --></span>
<span ><<span >load-on-startup</span>></span>1<span ></<span >load-on-startup</span>></span>
<span ></<span >servlet</span>></span>
<span ><<span >servlet-mapping</span>></span>
<span ><<span >servlet-name</span>></span>springMvc<span ></<span >servlet-name</span>></span>
<span ><!--
*.action 代表拦截后缀名为.action结尾的
/ 拦截所有但是不包括.jsp
/* 拦截所有包括.jsp
--></span>
<span ><<span >url-pattern</span>></span>/<span ></<span >url-pattern</span>></span>
<span ></<span >servlet-mapping</span>></span>

<span ><!-- 配置Post请求乱码 --></span>
<span ><<span >filter</span>></span>
<span ><<span >filter-name</span>></span>CharacterEncodingFilter<span ></<span >filter-name</span>></span>
<span ><<span >filter-class</span>></span>org.springframework.web.filter.CharacterEncodingFilter<span ></<span >filter-class</span>></span>
<span ><<span >init-param</span>></span>
<span ><<span >param-name</span>></span>encoding<span ></<span >param-name</span>></span>
<span ><<span >param-value</span>></span>utf-8<span ></<span >param-value</span>></span>
<span ></<span >init-param</span>></span>
<span ></<span >filter</span>></span>
<span ><<span >filter-mapping</span>></span>
<span ><<span >filter-name</span>></span>CharacterEncodingFilter<span ></<span >filter-name</span>></span>
<span ><<span >url-pattern</span>></span>/*<span ></<span >url-pattern</span>></span>
<span ></<span >filter-mapping</span>></span>

</web-app>