史上最全SpringMVC+Hibernate配置文件

在考虑到方便后期维护和尽量减少框架配置文件的前提下将Spring和Hibernate的配置文件合并为一个,而SpringMVC单独用一个配置文件。


 web.xml 的加载顺序是:context-param -> listener -> filter -> servlet,所以SSH先struts再spring然后hibernate

spring-mvc.xml文件:
<pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
	<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
	<!-- 加载controller的时候,不加载service,因为此时事物并未生效,若此时加载了service,那么事物无法对service进行拦截 -->
	<context:component-scan base-package="org.jeecgframework.web.*">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
	</context:component-scan>

	<context:component-scan base-package="com.dispatch.*">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
	</context:component-scan>

	<!-- ApplicationContext -->
	<bean class="org.jeecgframework.core.util.ApplicationContextUtil"></bean>

	<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射, 配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="cacheSeconds" value="0" />
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
			</list>
		</property>
		<property name="webBindingInitializer">
			<bean class="org.jeecgframework.core.interceptors.MyWebBinding" />
		</property>
	</bean>
	<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
	<bean id="defaultViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:order="3">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="contentType" value="text/html" />
		<property name="prefix" value="/webpage/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		p:defaultEncoding="UTF-8">
		<property name="maxUploadSize">
			<value>104857600</value>
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>
	<!-- 方言 -->
	<bean id="dialect"
		class="org.jeecgframework.core.common.hibernate.dialect.DialectFactoryBean">
		<property name="dbType" value="${jdbc.dbType}" />
	</bean>
	<!-- 异常处理类 -->
	<bean id="exceptionHandler"
		class="org.jeecgframework.core.common.exception.MyExceptionHandler" />
	<!-- 系统错误转发配置[并记录错误日志] -->
	<bean
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="defaultErrorView" value="500"></property>   <!-- 默认为500,系统错误(error.jsp) -->
		<property name="defaultStatusCode" value="404"></property>
		<property name="statusCodes"><!-- 配置多个statusCode -->
			<props>
				<prop key="error">500</prop>  <!-- error.jsp -->
				<prop key="error1">404</prop>    <!-- error1.jsp -->
			</props>
		</property>
		<property name="exceptionMappings">
			<props>
				<!-- 这里你可以根据需要定义N多个错误异常转发 -->
				<prop key="java.sql.SQLException">dbError</prop> <!-- 数据库错误(dbError.jsp) -->
				<prop key="org.springframework.web.bind.ServletRequestBindingException">bizError</prop> <!-- 参数绑定错误(如:必须参数没传递)(bizError.jsp) -->
				<prop key="java.lang.IllegalArgumentException">bizError</prop>  <!-- 参数错误(bizError.jsp) -->
				<prop key="org.springframework.validation.BindException">bizError</prop>  <!-- 参数类型有误(bizError.jsp) -->
				<prop key="java.lang.Exception">unknowError</prop>  <!-- 其他错误为'未定义错误'(unknowError.jsp) -->
			</props>
		</property>
	</bean>
	<!-- 拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<bean class="org.jeecgframework.core.interceptors.EncodingInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<bean class="org.jeecgframework.core.interceptors.AuthInterceptor">
				<property name="excludeUrls">
					<list>
						<value>loginController.do?goPwdInit</value>
						<value>loginController.do?pwdInit</value>
						<value>loginController.do?login</value>
						<value>loginController.do?checkuser</value>
						<value>repairController.do?repair</value>
						<value>repairController.do?deleteAndRepair</value>
						<!-- caseinfo survey -->
						<value>monitoringCenterController.do?kcy</value>
						<value>surveyWxController.do?getUserInfo</value>
						<value>surveyWxController.do?getRegistList</value>
						<value>surveyWxController.do?getRegistDetail</value>
						<value>surveyWxController.do?updateLossStatus</value>
						<value>surveyWxController.do?updateSignStatus</value>
						<value>surveyWxController.do?updateLocation</value>
						<value>surveyWxController.do?getlnglat</value>
						<value>surveyWxController.do?getLocation</value>
						<value>surveyWxController.do?updateRegistStatus</value>
						<value>surveyWxController.do?sendWxMsg</value>
						<value>taCaseInfoController.do?gotodispatch</value>
					</list>
				</property>
			</bean>
		</mvc:interceptor>
	</mvc:interceptors>

	<!-- 智能表单配置引入 -->
	<import
		resource="classpath*:org/jeecgframework/web/cgform/common/spring-mvc-cgform.xml" />

</beans>

spring-hibernate文件:
 
 
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><?xml version="1.0" encoding="UTF-8"?></span>
<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:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	<!-- Spring+Hibernate配置 -->

	<!-- ==============================springMVC开始========================================= -->
	<context:component-scan base-package="cn.yznu.stc.dao,cn.yznu.stc.service">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<!-- ==============================springMVC结束========================================= -->

	<!-- =================================spring开始========================================= -->
	<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/stc?useUnicode=true&characterEncoding=utf8"></property>
		<property name="username" value="root" />
		<property name="password" value="mw130283" />
		<!-- 数据库连接池保持的最小连接数 -->
		<property name="minIdle" value="5" />
		<!-- 数据库连接池保持的最大连接数 -->
		<property name="maxIdle" value="30" />
		<!-- 当数据库连接因为某种原因断掉之后,再重新从连接池中拿另外一个连接时实际上这个连接可能 已经无效,所以为了确保所拿到的连接全都有效需要在获取连接,返回连接以及连接空闲时进行 
			有效性验证 下面3个设置为ture时进行验证,默认为false -->
		<!-- 取得连接时是否进行有效性验证 -->
		<property name="testOnBorrow" value="true" />
		<!-- 返回连接时是否进行有效性验证 -->
		<property name="testOnReturn" value="true" />
		<!-- 连接空闲时是否进行有效性验证 -->
		<property name="testWhileIdle" value="true" />
	</bean>
	<!-- =================================spring结束========================================= -->

	<!-- =================================hibernate开始====================================== -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="datasource" />
		<property name="hibernateProperties">
			<props>
				<!-- 设置Hibernate方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 是否打印sql -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 格式化sql -->
				<prop key="hibernate.format_sql">true</prop>
				<!-- 是否自动更新表 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>

				<!-- 最大抓取深度,如果为0,则关闭默认的外连接抓取。建议值为0-3 -->
				<prop key="hibernate.max_fetch_depth">1</prop>
				<!-- 用于生成有助于调试的注释信息,默认为关闭 -->
				<prop key="hibernate.use_sql_comments">true</prop>

			</props>
		</property>
		<!-- 注解方式配置 -->
		<property name="packagesToScan">
			<list>
				<value>cn.yznu.stc.pojo</value>
				<!-- more -->
			</list>
		</property>
	</bean>

	<!-- 配置事物管理器,在*ServiceImpl里写@Transactional就可以启用事物管理 -->
	<bean name="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
	<!-- =================================hibernate结束=================================== -->
</beans>


 
 
<pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size: 24px;">web.xml文件:</span></span>

 
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

	<!-- ====================================Spring开始======================================= -->
	<!-- 监听spring上下文容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring-hibernate.xml</param-value>
	</context-param>
	<!-- ====================================Spring结束======================================= -->

	<!-- ===================================SpringMVC开始===================================== -->
	<!-- 配置Spring MVC DispatcherServlet -->
	<servlet>
		<servlet-name>MVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 初始化参数 -->
		<init-param>
			<!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath*:spring-mvc.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 配置DispatcherServlet所需要拦截的 url -->
	<servlet-mapping>
		<servlet-name>MVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<!-- ====================================SpringMVC结束===================================== -->
</pre><pre name="code" class="html"><span style="white-space:pre"></span><pre name="code" class="html"><span style="white-space:pre">	</span><!-- ========<span style="font-size:24px;color:#ff0000;"><strong>以下内容可以不要,对环境的搭建没有影响(开始)</strong></span>======= -->
<!-- ===================================编码过滤器开始====================================== --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>*</url-pattern></filter-mapping><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>*</url-pattern></filter-mapping><!-- ====================================编码过滤器结束===================================== --><!-- ====================================乱码过滤器开始===================================== --><filter><filter-name>CharacterFilter</filter-name><filter-class>cn.yznu.stc.filter.CharacterFilter</filter-class></filter><filter-mapping><filter-name>CharacterFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>LoginFilter</filter-name><filter-class>cn.yznu.stc.filter.LoginFilter</filter-class></filter><filter-mapping><filter-name>LoginFilter</filter-name><url-pattern>*.jsp</url-pattern></filter-mapping><!-- ====================================乱码过滤器结束===================================== -->
<span style="white-space:pre">	</span><!-- ========<span style="font-size:24px;color:#ff0000;"><strong>以上内容可以不要,对环境的搭建没有影响(结束)</strong></span>======= -->
<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
 
 

你可能感兴趣的:(spring,mvc,Hibernate)