SSM集成

<!-- SpringMVC -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>3.2.8.RELEASE</version>
  </dependency>

  <!-- Spring-JDBC,要和spring-webmvc的版本一致 -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>3.2.8.RELEASE</version>
  </dependency>

  <!-- MyBatis -->
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.4.6</version>
  </dependency>

  <!-- MyBatis-Spring 集成jar包 -->
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>1.3.2</version>
  </dependency>

  <!-- MySQL驱动jar包 -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.28</version>
  </dependency>

  <!-- DBCP连接池 -->
  <dependency>
   <groupId>commons-dbcp</groupId>
   <artifactId>commons-dbcp</artifactId>
   <version>1.4</version>
  </dependency>

  <!-- 添加jackson,自动转换为JSON数据 -->
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.4</version>
  </dependency>

  <!-- 添加jstl标签库 -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>

  <!-- Junit -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
  </dependency>


  <!-- 导入aspectj依赖 -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>1.8.13</version>
  </dependency>

  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.8.13</version>
  </dependency>

  <!-- 导入spring的aop -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>3.2.8.RELEASE</version>
  </dependency>

  <!-- 添加文档上传的依赖 -->
  <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3.1</version>
  </dependency>

  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.4</version>
  </dependency>

配置数据库连接信息 — db.properties文档

url=jdbc:mysql://localhost:3306/db_blog3?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
user=root
password=root
initSize=2
maxSize=10

Mybatis和Spring集成 — spring-dao.xml

  • 读取 db.properties 文档配置 DBCP 连接池创建数据源
  • 使用上面的数据源配置 SqlSessionFactoryBean
  • 配置组件扫描Mybatis的接口 mapper 的包,用于创建mapper接口对象
  • 配置批量扫描Mybatis的 XXMapper.xml 文档的 MapperScannerConfigurer
  • 配置事务管理器
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

 <!-- 组件扫描 用于自动创建mapper包下的所有接口的对象,否则将不能使用@Resource注解的方式注入接口对象 -->
 <context:component-scanbase-package="cn.tedu.blog.mapper"/>
 
 <!-- 配置MapperScannerConfigurer -->
 <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <!-- 用于配置持久层接口在哪里,指定mapper的包 -->
  <propertyname="basePackage"value="cn.tedu.blog.mapper"/>
 </bean>
 
 <!-- 加载db.properties,其中定义了数据库的配置信息 -->
 <util:propertiesid="dbConfig"location="classpath:db.properties"/>

 <!-- 数据源 -->
 <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
  <propertyname="url"value="#{dbConfig.url}"/>
  <propertyname="driverClassName"value="#{dbConfig.driver}"/>
  <propertyname="username"value="#{dbConfig.user}"/>
  <propertyname="password"value="#{dbConfig.password}"/>
  <propertyname="initialSize"value="#{dbConfig.initSize}"/>
  <propertyname="maxActive"value="#{dbConfig.maxSize}"/>
 </bean>


 <!-- 配置SqlSessionFactoryBean -->
 <beanclass="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 用于配置数据库连接池 -->
  <propertyname="dataSource"ref="dataSource"/>
  <!-- 用于配置持久层映射文档在哪里,所有的xml文档,使用通配符 -->
  <propertyname="mapperLocations"value="classpath:mappers/*.xml"/>
 </bean>
 

 <!-- 配置事务管理器 -->
 <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- 注入数据源,这里使用的是上面配置好的DataSource -->
  <propertyname="dataSource"ref="dataSource"></property>
 </bean>
 
 <!-- 开启事务注解 ,transaction-manager指定的是上面配置的事务管理器的id-->
 <tx:annotation-driventransaction-manager="transactionManager"/>
</beans>

Spring与SpringMVC不需要集成

springMVC.xml
controller
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

 <!-- 组件扫描 自动创建对象 -->
 <context:component-scanbase-package="cn.tedu.blog.controller"/>

 <!-- 配置ViewResolver视图解析器 -->
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <propertyname="prefix"value="/"/>
  <propertyname="suffix"value=".jsp"/>
 </bean>

 <!-- 配置驱动,用于@ResponseBody的使用 -->
 <mvc:annotation-driven></mvc:annotation-driven>

 <mvc:interceptors>
  <mvc:interceptor>
   <!-- 配置拦截器的路径 -->
   <mvc:mappingpath="/blogger/*"/>
   <mvc:mappingpath="/blogType/*"/>
   <!-- 配置不拦截的路径 -->
   <mvc:exclude-mappingpath="/blogger/showLogin.do"></mvc:exclude-mapping>
   <mvc:exclude-mappingpath="/blogger/login.do"></mvc:exclude-mapping>
   <mvc:exclude-mappingpath="/blogger/showInfo.do"></mvc:exclude-mapping>

   <beanclass="cn.tedu.blog.interceptor.LoginInterceptor"></bean>
  </mvc:interceptor>
 </mvc:interceptors>
 
 <!-- 上传组件的解析器 -->
 <beanid="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!-- 上传文档大小 -->
  <propertyname="maxUploadSize"value="10000000"></property>
  <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
  <propertyname="defaultEncoding"value="utf-8"></property>
 </bean>

</beans>

业务层的配置文档 – spring-service.xml

  • 配置组件自动扫描业务层的类,自动创建对象
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

 <!-- 组件扫描 -->
 <context:component-scanbase-package="cn.tedu.blog.service"/>

</beans>

配置 web.xml 文档

  • 配置 POST 中文乱码过滤器
  • 配置 Spring 的监听器 ContextLoaderListener ,用于在容器启动的时候就加载spring的配置文档
  • 配置 SpringMVC 的前端控制器 DispatcherServlet ,其中指定的是springMVC的配置文档 springMVC.xml
<!-- 解决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>
 </filter>
 <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- 配置SpringMVC的前端控制器 -->
 <servlet>
  <servlet-name>SpringMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <!-- springmvc的配置文档 -->
   <param-value>classpath:springMVC.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 <!-- 使用ContextLoaderListener配置spring的监听器,主要是在启动的时候加载spring的配置文档 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
   <!-- 指定所有的spring配置文档,这里使用 * 通配符 -->
   <param-value>classpath:spring-*.xml</param-value>
 </context-param>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>