配置
整合SSM最终的目的是在Tomcat启动的时候带动三个框架实现良性循环。这其中涉及到四个配置文件web.xml
、applicationContext.xml
、springmvc.xml
和sqlMapConfig.xml
。
首先SSM框架的核心是Spring,所以应该是在applicationContext.xml
中配置springmvc.xml
和sqlMapConfig.xml
,但是Spring MVC和Spring是一家的,所以只需要把Mybatis配置进去就可以。
第一步:在applicationContext.xml
配置Mybatis和数据库连接池:配置sqlSessionFactoryBean和sqlMapper接口所在的包。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 引入数据库信息properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- Mybatis的工厂(不同的项目只需要改configLocation的value) -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<!-- Mapper动态代理开发(不同的项目只需要改basePackage的value) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 基本包,com.sm.mapper是sqlMap接口的根包,它下面及其子包下面的sqlMap接口都会被扫描到 -->
<property name="basePackage" value="com.ssm.mapper"/>
</bean>
</beans>
第二步:配置sqlMapConfig.xml
:由于数据库连接池交给Spring管理并且使用动态Mapper开发,所以这个文件的作用就是配置别名来帮我们简便开发。
<?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>
<!-- 设置别名 -->
<typeAliases>
<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="com.itheima.springmvc.pojo" />
</typeAliases>
</configuration>
第三步:配置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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描@Controler @Service -->
<context:component-scan base-package="com.ssm.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>
第四步:配置web.xml
文件:Tomcat开启时会加载web.xml
。Mybatis配置文件已经被配置进applicationContext.xml
中,所以在web.xml
中需要配置springmvc.xml
和applicationContext.xml
。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm-1</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>
<!-- 配置Spring上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring监听器,就是加载 applicationContext.xml文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器 -->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
流程分析
当服务器开启时,web.xml
会被加载进去,就会把SSM框架的配置文件都加载进去,使用注解开发时在bean类上加上相应注解就可以。对于Mybatis的动态代理开发,虽然都是接口,不能使用注解,但在applicationContext.xml
中已经配置了扫描,会自动帮我们创建Spring的bean,使用注解@Autowired
就可以自动注入属性。对于其他的bean
,使用@Component
或@Service
或@Controller
注解标识一下就可以。