现在市面上spring+springMVC+mybatis是绝对主流,新项目几乎都是这套框架,所以SSM的整合成了项目初期最先考虑的事情。能力有限,不看源码,只能浅显地总结一下SSM框架整合过程。

Spring

提到Spring就会想到它的两大特性AOP和DI(依赖注入),也就是IOC容器。

首先了解一下Spring框架的三大核心组件:Core、Context和Bean。

  1. Bean在Spring中的概念就等同于对象在Java中的概念。Spring可以把对象之间的关系转而用配置文件来管理,这就是依赖注入机制。而这个注入关系在一个叫IOC的容器中管理。IOC容器就是被Bean包裹的对象。Spring通过把对象包装在Bean中从而达到管理这些对象及做一系列额外操作。

  2. Context为Bean提供了生存环境,它建立起了每个Bean之间的干系,并且维护这种关系

  3. Core可以理解为一个Util,他是Context建立和维护Bean之间关系所需要的工具一系列工具。

web.xml

下面是一个简单工程的web.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>xxx-web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决post乱码 -->
<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>
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>xxx</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>xxx</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

web.xml文件是用来初始化配置信息的,比如Welcome页面、servlet、filter、listener、启动加载级别等。

  1. ssm框架整合,第一件事情就是加载spring容器,ContextLoaderListener监听器的作用就是启动web容器是,自动装配ApplicationContext的配置信息,从而生成spring的context对象,就是spring的环境。我们在写单元测试的时候就可以用以下代码生成一个ApplicationContext对象,其中”xxx.xml”就是你要加载的配置信息。

    1
    ApplicationContext act = new ClassPathXmlApplicationContext("xxx.xml");
  2. springmvc前端控制器,用来拦截匹配请求,load-on-startup设置加载级别为1及最高级别,也就是前端发过来的请求首先进入到这个DispatcherServlet中,然后根据springmvc.xml的配置,进入到springmvc的流程中去。

Springmvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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: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/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.xsd">
<context:component-scan base-package="xxx.controller" />
<mvc:annotation-driven />
<!-- 视图解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

自动扫描springmvc的controller,这里是加载到springmvc的IOC容器之中,mvc:annotation-driven 就是使用注解。视图解析器这里使用的是JSP页面,现在市面上出现了许多模板引擎,例如velocity、Freemarker等等,这些模板引擎的出现也同时导致了JSP的没落,因为它们实现JSP的绝大数功能,而且在效率上要强过JSP。

application-Xxx.xml

Sping 的配置文件可以分为三个部分,第一是表现层的整合,及上面的springmvc.xml,另外两个部分:

  1. Dao层,主要用来整合Mybatis,具体的思路可以分为三个步骤:
    第一、配置数据库连接池(首先得加载数据库配置文件),淘宝的druid数据库连接池可以说最好的一个,还有其它的数据库连接池,比如C3P0等等;
    第二、配置SQLSessionFactoryBean,加载数据库连接池和Mybatis的配置文件;
    第三、配置MapperScannerConfigurer,用来加载Mybatis的Mapper文件。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    <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">
    <!-- 数据库连接池 -->
    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:properties/*.properties" />
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
    destroy-method="close">
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="maxActive" value="10" />
    <property name="minIdle" value="5" />
    </bean>
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据库连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 加载mybatis的全局配置文件 -->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="xxx.mapper" />
    </bean>
    </beans>

mybatis-config.xml

在这个文件是用来配置mybatis的,例如可以加载驼峰命名法,配置第三方缓存等等

1
2
3
4
5
6
7
8
9
10
<?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>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>

db.properties

驱动,URL,用户名,密码,还包括maxActive,minIdle等等

1
2
3
4
5
6
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xxxx?characterEncoding=utf-8
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin@localhost:1521:xxxx
jdbc.username=****
jdbc.password=****

  1. service层,又可以分两部分

    1. 管理service的实现类

      1
      2
      <!-- 自动扫描service包-->
      <context:component-scan base-package="xxx.service"/>
    2. 管理事务
      第一、加载事务管理器
      第二、通知,配置事务的传播行为
      PROPAGATION_REQUIRED–支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
      PROPAGATION_SUPPORTS–支持当前事务,如果当前没有事务,就以非事务方式执行。
      PROPAGATION_MANDATORY–支持当前事务,如果当前没有事务,就抛出异常。
      PROPAGATION_REQUIRES_NEW–新建事务,如果当前存在事务,把当前事务挂起。
      PROPAGATION_NOT_SUPPORTED–以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
      PROPAGATION_NEVER–以非事务方式执行,如果当前存在事务,则抛出异常。
      第三、切面
      pointcut=”execution( xxx.service..(..))” 第一个匹配任何返回值,后两个*匹配service包及其子包下任何方法,(..)匹配方法的任何参数类型

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      <!-- 事务管理器 -->
      <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="save*" propagation="REQUIRED" />
      <tx:method name="insert*" propagation="REQUIRED" />
      <tx:method name="add*" propagation="REQUIRED" />
      <tx:method name="create*" propagation="REQUIRED" />
      <tx:method name="delete*" propagation="REQUIRED" />
      <tx:method name="update*" propagation="REQUIRED" />
      <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
      <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
      <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
      </tx:attributes>
      </tx:advice>
      <!-- 切面 -->
      <aop:config>
      <aop:advisor advice-ref="txAdvice"
      <!-- 切入点表达式 -->
      pointcut="execution(* xxx.service.*.*(..))" />
      </aop:config>

简单说了一下ssm框架的搭建过程,写完以后发现并没有达到我想要的效果,能力有限啊。。。。