SSM整合

SpringMVC+Spring+MyBatis整合

SpringMVC.xml

<!-- 注解包扫描-->
<context:component-scan base-package="xxcontroller" use-default-filters="false">
    <context:include-filter type="annotation" expression="xx.Controller"/>
</context:component-scan>
<!-- 静态资源-->
<mvc:default-servlet-handler/>
<!-- 请求处理器(json)-->
<mvc:annotation-driven>
	<mvc:message-converters>
			<bean class="xx.FastJsonHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json</value>
					</list>
				</property>
			</bean>
	</mvc:message-converters>
</mvc:annotation-driven>
<!-- 试图解析器-->
<bean class="xx.InternalResourceViewResolver">
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
</bean>
<!-- 文件下载-->
<bean id="multipartResolver" class="xx.commons.CommonsMultipartFile"/>
<!-- 拦截器-->
<mvc:interceptors>
		<mvc:interceptor>
            <!-- 过滤-->
			<mvc:mapping path="/per/**" />
			<!-- 不过滤-->
            <mvc:exclude-mapping path="/head/*" />
			<bean class="xxx"/>
		</mvc:interceptor>
</mvc:interceptors>

web.xml

<!-- mvc 前端控制器 -->
<servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>xx.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>mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- mvc 字符编码过滤器 -->
<filter>
    <filter-name>encoding</filter-name>
    <filter-class>xx.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 指定Spring.xml位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
</context-param>
<!-- 配置spring工厂 创建监听器 -->
<listener>
    <listener-class>xx.ContextLoaderListener</listener-class>
</listener>

spring.xml

<!-- 引入db配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 注解包扫描 -->
<context:component-scan base-package="com.ssm"
                        use-default-filters="true">
    <context:exclude-filter type="annotation"
                            expression="x.Controller" />
</context:component-scan>
<!-- 配置数据源 测试点1 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${oracle.driver}" />
    <property name="jdbcUrl" value="${oracle.url}" />
    <property name="user" value="${oracle.name}" />
    <property name="password" value="${oracle.password}" />
</bean>
<!-- sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 	<!-- 测试点2 -->
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath:/mapper/*.xml" />
    <!-- <property name="typeAliasesPackage" value="com.ssm.entity" /> -->
</bean>
<!-- dao扫描dao包 -->
<bean class="xx.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <property name="basePackage" value="com.ssm.dao" />
</bean>
<!-- 事务控制datasorecetranscationManager -->
<bean id="transactionManager"  class="xx.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<!-- 注解生效 -->
<tx:annotation-driven transaction-manager="transactionManager" />