概述
SSM(Spring,SpringMVC,MyBatis),SpringMVC更轻量级,性能更好,以方法作为处理业务的最小单元,且能够与Spring完美整合,Spring在项目管理方面依旧独当一面。
MyBatis 相比Hibernate而言,对开发人员而言更加透明,定制性更强,性能方面更显著。
SSM整合流程
1.角色扮演
SpringMVC管理控制器负责处理请求,MyBatis负责与数据表相关的操作,Spring 管理Service及数据模型的创建,Spring提供申明式事物管理。
2.SSM开发环境搭建流程
①倒入各类Jar包
.MyBatis + mybatis-spring-1.2.4.jar +commons-logging.jar +C3p0
.Spring + aspectj-1.8.7.jar + aopalliance-1.0.jar
.SpringMVC
②Spring 环境部署
.创建ApplicationContext.xml Bean配置文件
.web.xml中配置Spring Bean配置文件路径
<context-param>
<param-name>contextConfigLocation</param-name>
<!--Spring Bean 配置文件路径-->
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
.web.xml中配置ContextLoaderListener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
.配置Spring IOC容器只扫描Service和Model
<context:component-scan base-package="cn.com.zjf">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
③MyBatis环境部署
.创建db.properties数据源属性
db.user=root
db.pass=root
db.driverClass=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/spring_4
.配置C3p0数据源
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${db.user}"></property>
<property name="password" value="${db.pass}"></property>
<property name="driverClass" value="${db.driverClass}"></property>
<property name="jdbcUrl" value="${db.url}"></property>
</bean>
.配置SQLSessionFactory Bean对象
<!-- 配置Session Factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--MyBatis配置文件路径-->
<property name="configLocation" value="classpath:config.xml"></property>
<!--映射文件匹配-->
<property name="mappingLocations" value="classpath:cn/com/zjf/**/*.xml"></property>
</bean>
.配置Spring申明式事物
<pre name="code" class="html"><!-- 配置事物管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事物注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager" />
.配置Spring自动扫描MyBatis接口映射文件
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="cn.com.zjf"></property>
</bean>
④Spring MVC 环境部署
.创建SpringMVC配置文件springmvc.xml
.配置SpringMVC只扫描控制器
<context:component-scan base-package="cn.com.zjf"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
.配置视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
.处理静态资源
<mvc:default-servlet-handler />
<mvc:annotation-driven />
.配置SpringMVC前端控制器及IOC容器配置文件
<servlet>
<servlet-name>springDispatcherServlet</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>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
常见问题汇总
1.配置文件中XMl文件配置时是否指定类路径classpath:
2.Spring IOC容器和SpringMVC IOC容器的关系
i. Spring IOC容器包含了 SpringMVC容器
ii. Spring IOC 容器中的Bean可以依赖SpringMVC IOC容器中的Bean反之不行
附件
1.项目结构