JavaWeb——Spring、SpringMVC、Mybatis整合

整合SSM框架需要对各个配置文件进行修改

web.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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>2019-8-30</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>
  
  <!-- SpringMVC传参乱码解决方案 -->
  <filter>
  	<filter-name>EncodingFilter</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>EncodingFilter</filter-name>
	  		<url-pattern>/**</url-pattern>
  </filter-mapping>
  
  <!-- spring的监听器 -->
  <listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置一个前端Servlet控制器 -->
  <servlet>
			  <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
			  <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index</welcome-file>
  </welcome-file-list>
</web-app>

SpringMVC.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd ">
	
	<!-- 增加包扫描 -->
	<context:component-scan base-package="beer.onexone.controller"/>
	
	<!-- 自动配置参数 --><!-- 添加自定义转换类 -->
	<mvc:annotation-driven   conversion-service="dateConvert"/>
	
	<!-- 开启静态资源自动放行 -->
	<mvc:default-servlet-handler/>
	<!-- 也可以手动放行	 -->
		<mvc:resources location="/	 UploadOrDownload/" mapping="/UploadOrDownload/**"></mvc:resources>

		<!-- 配置拦截器 -->
		<mvc:interceptors>
				<mvc:interceptor>
				<!-- 拦截路径设置 -->
						<mvc:mapping path="/**"/>
					<!-- 配置拦截器类 -->
						<bean class="beer.onexone.utils.TheHandlerInterceptor"/>
				</mvc:interceptor>
		</mvc:interceptors>
		
	<!-- 统一异常处理配置 -->
	 <bean class="beer.onexone.utils.TheHandlerExcetpionResolver"/>
	 
	 
	 <!-- 文件上传配置 -->
		<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 编码类型 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- 总的上传文件最大值 -->
		<property name="maxUploadSize" value="10240000"></property>
		</bean>
		
	<!-- 配置视图解析器 -->                                                                                                                                                         
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<!-- 添加请求地址前缀 -->
		<property name="prefix" value="/CallBack/"></property>
		<!-- 添加请求地址后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 添加自定义的转换器 -->
	<bean id="dateConvert" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 	添加转换器 -->
		<property name="converters">
						<list>
							<bean class="beer.onexone.utils.ConvertDate"></bean>
						</list>
		</property>
	</bean>

	</beans>

Mybatis.xml(配置基本都被Spring接管)

<?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">

<!-- 整合之后Mybatis的配置文件都在Spring里面配置即可 -->
<configuration>
<typeAliases>
	<package name="beer.onexone.beans"/>
</typeAliases>

</configuration>

applicationContext.xml (Spring)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://www.springframework.org/schema/beans" 
	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-4.1.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.1.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.1.xsd ">
	
	<!-- 使用注释依赖注入 -->
	<context:annotation-config/>
	<!-- 添加包扫描 -->
	<context:component-scan base-package="beer.onexone">
			<!-- 不扫描Controller -->
		<context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	<!-- 读取prop文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- c3p0的配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driverClass}"/>
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="user" value="${username}"></property>
		<property name="password" value="${password}"></property>
		<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="${initialPoolSize}"/>
		<!-- 连接池中保留的最小连接数。 -->
		<property name="minPoolSize" value="${minPoolSize}"/>	
		<!-- 连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${maxPoolSize}"/>
		<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${maxIdleTime}"/>	
		<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${acquireIncrement}"/>	
		<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>
	</bean>
	
	<!-- Spring整合Mybatis -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- 配置mybaits的别名 -->
		<property name="typeAliasesPackage" value="beer.onexone.beans"></property>
	</bean>
	<!-- 配置mapper包扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="beer.onexone.mapper"></property>
	</bean>
	
	<!-- 添加一个事务管理平台spring官方自带 -->
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
		<!-- 配置事务 属性  方法  隔离级别  传播特性  是否只读 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="user*"  propagation="REQUIRED" read-only="false"/>
			<tx:method name="add*"  propagation="REQUIRED" read-only="false"/>
			<tx:method name="insert*"  propagation="REQUIRED" read-only="false"/>
			<tx:method name="save*"  propagation="REQUIRED" read-only="false"/>
			<tx:method name="update*"  propagation="REQUIRED" read-only="false"/>
			<tx:method name="delete*"  propagation="REQUIRED" read-only="false"/>
			<tx:method name="dorp*"  propagation="REQUIRED" read-only="false"/>
			<tx:method name="find*"  propagation="NOT_SUPPORTED" read-only="true"/>
			<tx:method name="get*"  propagation="NOT_SUPPORTED" read-only="true"/>
			<tx:method name="select*"  propagation="NOT_SUPPORTED" read-only="true"/>
			<tx:method name="load*"  propagation="NOT_SUPPORTED" read-only="true"/>
		</tx:attributes>
	</tx:advice>
		
			<!-- 将事务配置到项目中 -->
	<aop:config>
		<!-- 配置表达式 -->
		<aop:pointcut expression="execution(* beer.onexone.service.**.*Impl.*(..))" id="po"/>
		<!-- 将事务插入到项目 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="po"/>
	</aop:config>
	
</beans>

jingsongchan

发表评论

电子邮件地址不会被公开。 必填项已用*标注