《经久不衰的Spring框架:Spring+SpringMVC+MyBatis 整合》

前言

  主角即Spring、SpringMVC、MyBatis,即所谓的SSM框架,大家应该也都有所了解,概念性的东西就不写了,有万能的百度。之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些。以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下。个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助。 

  工作环境:JDK 1.7、Mysql 5.6、Myeclipse 10、Tomcat 7、Maven

  框架版本:Spring 4.2.6.RELEASE、SpringMVC 4.2.6.RELEASE、MyBatis 3.2.8

  

一、整体文件结构设计

项目目录结构:

      

 

  1、类文件和资源文件分开存放,类文件分为六层,其中common存放公共和框架部分,controller存放项目控制层,service存放项目业务逻辑层,model存放项目实体类,mapper存放数据层接口,test存放测试相关类。

  注:若涉及到多业务模块的情况,分层可以在各层内部进行划分,当然对于大模块建议采用Maven多模块项目方式搭建。

  2、前端在webapp下分出了common和project,common存放公共部分,project内部用于存放多个子模块,common以及各子模块内部分别新建images,js,css和view四个文件夹,用于归类不同资源。

 

二、项目入口web.xml

  构建好整体结构后,接下来应该把目光看下web.xml文件,无论是何种JavaEE框架,入口总是在web.xml,认准这边就没错了。

  web.xml 内容很多,SSM整合相关的关键部分就是加载Spring配置文件spring-config.xml,以及将SpringMVC核心调度器DispatcherServlet注册为servlet(配置文件为mvc-config.xml),其他部分有注释,选择性观看即可。

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  
    <!-- ================站台名称,站台描述,大小图标================ --> 
    <display-name>SpringMVC</display-name>
    <description>柴可夫斯基模板</description>
      <icon>
         <small-icon>/common/images/favicon.ico</small-icon>
       <large-icon>/common/images/favicon.ico</large-icon>
    </icon>
  
    <!-- 支持分布式 -->
    <!-- <distributable/> -->

    <!-- 应用路径,如果不设置,缺省为"webapp.root",当tomcat加载多个项目时,会发生名称冲突 -->  
    <context-param>  
        <param-name>webAppRootKey</param-name>  
        <param-value>spring4.root</param-value>  
    </context-param> 
    
    <!-- ================log4j配置开始================ --> 
    <!-- log4j2不需要在这边做额外的配置 -->
    <!-- ================log4j配置结束================ --> 
    
    <!-- ================Spring配置开始================ --> 
    <!-- 设置Spring容器加载所有的配置文件的路径 -->
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring-config.xml</param-value>
      <!-- <param-value>classpath:/spring-*.xml</param-value> -->
    </context-param>
    <!-- 配置Spring监听器,可以在容器启动时,加载contextConfigLocation的context-param节点的配置文件 -->
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- ================Spring配置结束================ -->
    
    
    <!-- 配置监听器,定义在服务器启动和关闭时,需要执行的方法 -->
    <listener>
      <listener-class>com.demo.common.startup.InitListener</listener-class>
    </listener>
    
    <!-- 防止Struts和Quartz等内存溢出监听器 -->
    <listener>
      <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    
    <!-- 监听HTTP请求事件,为Bean的request,session,globalsession等作用域提供支持 -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    
    <!-- Spring 字符编码配置 --> 
    <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>
      <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>characterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  
    <!-- 自定义登录过滤器,拦截JSP页面,不允许直接访问 -->
    <filter>
      <filter-name>loginFilter</filter-name>
      <filter-class>com.demo.common.filter.LoginFilterSpring</filter-class>
      <init-param>
        <param-name>charset</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
        <param-name>contentType</param-name>
        <param-value>text/html;charset=UTF-8</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>loginFilter</filter-name>
      <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    
    <!--包装request过滤器-->
    <filter>
        <filter-name>wrapRequestFilter</filter-name>
        <filter-class>com.demo.common.filter.WrapParameterFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>wrapRequestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 激活Tomcat的defaultServlet来处理静态文件(效率较高) -->  
    <servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.jpg</url-pattern>
      <url-pattern>*.gif</url-pattern>
      <url-pattern>*.png</url-pattern>
      <url-pattern>*.js</url-pattern>
      <url-pattern>*.css</url-pattern>
      <url-pattern>*.ico</url-pattern>
      <url-pattern>*.eot</url-pattern>
      <url-pattern>*.svg</url-pattern>
      <url-pattern>*.ttf</url-pattern>
      <url-pattern>*.woff</url-pattern>
      <url-pattern>*.mp3</url-pattern>
      <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    
    <!-- ================配置SpringMVC核心调度器================ -->
    <!-- 不指定具体文件的话,默认为"/WEB-INF/<servlet name>-servlet.xml" -->
    <!-- load-on-startup代表启动顺序,设置为大于等于0表示容器在应用启动时就加载并初始化这个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*:mvc-config.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>SpringMVC</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 随服务器启动的servlet --> 
    <servlet>
      <servlet-name>initServlet</servlet-name>
      <servlet-class>com.demo.common.startup.InitServlet</servlet-class>
      <load-on-startup>0</load-on-startup>
    </servlet>
    
    <!-- 阿里巴巴数据源配置启用Web监控统计功能 -->
    <!-- 通过 http://ip:port/druid/ 地址访问即可 -->
    <servlet>
      <servlet-name>DruidStatView</servlet-name>
      <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
      <init-param>
            <param-name>allow</param-name>
            <param-value>127.0.0.1</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
      <servlet-name>DruidStatView</servlet-name>
      <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    
    <!-- CXF服务发布配置 -->
    <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup> 
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>
    
    <!-- 设置session过期时间为60分钟 -->
    <session-config>
      <session-timeout>60</session-timeout>
    </session-config>
    
    <!-- 指定错误404和500的处理页面 -->
    <error-page>
      <error-code>404</error-code>
      <location>/common/view/404.jsp</location>
    </error-page>
    <error-page>
      <error-code>500</error-code>
      <location>/common/view/500.jsp</location>
    </error-page>
    
    <!-- 欢迎页面 -->
    <welcome-file-list>
      <welcome-file>common/view/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 

三、Spring 框架配置

这块涉及内容也很多,从何说起呢,先上一下文件内容好了:

spring-config.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    
    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:constant.properties"/>
    
    <!-- 开启异步任务(同时开启定时器注解扫描) -->
    <task:annotation-driven />
    
    <!-- 使用@AspectJ风格的切面声明 -->
    <!-- <aop:aspectj-autoproxy/> -->
    
    <!-- 使用Annotation自动注册Bean -->
    <!-- 在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解 -->
    <context:component-scan base-package="com.demo"><!-- base-package 如果多个,用“,”分隔 --> 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>  
    
    <!-- 引入Mybatis配置 -->
    <import resource="spring-mybatis.xml"/>
    
    <!-- 引入Socket配置 -->
    <import resource="spring-socket.xml"/>
    
    <!-- 引入MongoDB配置 -->
    <import resource="test/mongo-config.xml"/>
    
    <!-- 引入定时器任务配置 -->
    <!-- <import resource="classpath*:com/demo/config/spring-job.xml"/> -->
    
    <!-- 引入hibernate4配置 -->
    <!-- <import resource="spring-hibernate.xml"/> -->
    
    <!-- 引入缓存配置 -->
    <!-- <import resource="spring-cache.xml"/> -->
    
    <!-- 引入CXF配置 -->
    <import resource="test/spring-cxf.xml"/>
    
    <!-- 引入Redis配置(无需如此配置,直接使用RedisUtil即可) -->
    <!-- <import resource="test/spring-jedis.xml"/> -->
</beans>

上面内容很多,但是并不全是SSM框架的,需要关注的点只有下面几个:

1、context:component-scan 包扫描

  这个注解不用多说了,要注意的就是主容器中不扫描@Controller注解,因为@Controller将会在SpringMVC扫描。

2、import 标签和多文件配置

  在团队开发时候,每个人都常去改动Spring配置文件,不科学,使用这个技巧方便,每个都有各自的配置文件了。项目较大,有较多的bean时,可以将其分散到子文件中。虽然Spring还有自动扫描的功能,但我感觉也不怎么好,需要去扫描,影响性能;而且各个Bean分散在不同包中,不好配置。

  多文件配置通常有两种做法:

  2.1 在 web.xml配置中的contextConfigLocation节点配置多个值。

具体代码如下:

<context-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value> 
      /WEB-INF/classes/context1.xml, 
       /WEB-INF/classes/context2.xml, 
       /WEB-INF/classes/context3.xml    
    </param-value> 
</context-param> 

  其中分隔符可以是","也可以是" "等,也可以用通配符application-*,这样配置的要求是,你的Spring配置文件必须是applicationContext-*****.xml这样的形式存在,*号代表通配符,具体就不说了。 

     2.2 在一个application.xml中配置多个import标签引入其他文件。

  个人喜欢这种方式,清晰明了,总得有一个主入口吧(估计受了webpack和SeaJS的影响)。

  从这边也不难看到Spring框架在其中扮演的角色:管理容器,有效地组织你的中间层对象(无节操得集成其他框架)。

3、引入mybatis配置:spring-mybatis.xml

  这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。

spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    
    <!-- 1. 数据源配置 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" />
    
    <!-- Druid方式配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
    
        <!-- 基本属性 url、user、password -->
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="10" />  
        <property name="minIdle" value="20" />  
        <property name="maxActive" value="100" />  
        
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />  
    
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="6000" />  
        
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />  
    
        <!-- 验证是否回收 -->
        <property name="validationQuery" value="SELECT 'x' FROM DUAL" />  
        <property name="testWhileIdle" value="true" />  
        <property name="testOnBorrow" value="false" />  
        <property name="testOnReturn" value="false" /> 
    
        <!-- 开启Druid的监控统计功能 -->  
        <property name="filters" value="stat" />  
    </bean> 
    
    <!-- 2. 创建SqlSession的工厂 -->
    <!-- dataSource:引用数据源,统一加载配置--> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

        <property name="dataSource" ref="dataSource" ></property>    

        <!-- 自动配置别名-作用类似mybatis-config.xml的别名 -->  
        <property name="typeAliasesPackage" value="com.demo.model" />
        
        <!-- 设置别名的类加上父类限定 -->  
        <property name="typeAliasesSuperType" value="com.demo.common.base.BaseEntity"/>
        
        <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径 -->   
        <!-- *是个通配符,代表所有的文件,**代表所有目录下 --> 
        <property name="mapperLocations" value="classpath*:mappings/**/*.xml"/>
        
        <!-- 指定mybatis核心配置文件 --> 
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>  

    <!-- 3. 自动扫描加载Sql映射文件/接口 -->
    <bean id="mapperScannerConfigurer"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <!-- sqlSessionFactoryBeanName:代表延迟加载-->
        <!-- 这个配置的前提条件是:映射接口类文件(.java)和映射XML文件(.xml)需要放在相同的包下(com.demo.mapper)-->
        <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

        <!-- basePackage:指定sql映射文件/接口所在的包(自动扫描)-->
        <property name="basePackage" value="com.demo.mapper"></property>
    
        <!-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->
        <property name="annotationClass" value="com.demo.common.persistence.annotation.MyBatisDao"/>
    </bean>
    
    <!-- 4. 事务管理 --> 
    <!-- dataSource:引用上面定义的数据源 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 5. 使用声明式事务 -->
    <!-- transaction-manager:引用上面定义的事务管理器 -->
    <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务  -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    
    <!-- 定义JdbcTemplate的Bean -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"></bean>
    
</beans>

 

四、SpringMVC 框架配置

  这块配置里面的注释也很详细,在此就不说了,主要是自动扫描控制器,视图模式,注解的启动这三个。

mvc-config.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    
    <!-- 打开使用注解自动检测功能自动注册Bean,只扫描@Controller -->
    <context:component-scan base-package="com.demo"  use-default-filters="false">      
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />    
    </context:component-scan>  
    
    <!-- 文件上传表单的视图解析器 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding" value="utf-8"></property>   
        <property name="maxUploadSize" value="2097152"></property><!--限制文件上传2M内  -->   
        <property name="maxInMemorySize" value="40960"></property>  
    </bean>  
    
    <!-- 默认的注解映射的支持 - 增加String类型中文解析 -->  
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <!-- 使用自定义Spring拦截器 -->
    <mvc:interceptors>    
      <mvc:interceptor>    
            <mvc:mapping path="/**" />     
            <bean class="com.demo.common.core.SpringMVCInterceptor"></bean>    
        </mvc:interceptor>    
    </mvc:interceptors>    
    
    <!-- 视图配置 -->
    <!-- 对转向页面的路径解析,指定输出视图的前后缀,controller返回的视图直接加上此前后缀 --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/project/" p:suffix=".jsp" />
        
</beans>

  这部分内容也可以参考上一篇博文:《经久不衰的Spring框架:SpringMVC 统括》

 

五、部署与测试

  到此,已经完成了SSM三大框架的整合了,接下来测试一下,如果成功了,那么恭喜你,如果失败了,继续调试吧,作为程序员就是不停的与BUG做斗争!

    测试的话,无非就是新建对应的view、controller、service、mapper ,测试一下SpringMVC和Mybatis的功能,很简单,就不罗嗦了。

  部署的话,就干到tomcat里面去,启动,访问localhost即可,也不赘述了。

  至此,SSM三大框架的整合就完成了,在此基础上可再添加其他功能。

 

编后语

  由于这只是SSM整合的文章,所以上面很多文件内容没有面面俱到,比如日志处理、socket、ws、bean定制等等,后续觉得有用得会继续放上来,请继续关注山人行博客。

posted @ 2017-02-28 16:31  山人行  阅读(13256)  评论(3编辑  收藏  举报