SpringMVC+Spring+Hibernate调整

SpringMVC+Spring+Hibernate整合

1.所需要jar包:

SpringMVC+Spring+Hibernate调整


web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
	
	  <!-- 配置DispatcherServlet -->
	  <servlet>
	  	<servlet-name>commServlet</servlet-name>
	  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  	<!-- 手动配置加载的配置文件 -->
	  	<init-param>
	  		<param-name>contextConfigLocation</param-name>
	  		<param-value>/WEB-INF/classes/spring/*.xml</param-value>
	  	</init-param>
	  	<!-- 项目启动时加载 -->
	  	<load-on-startup>1</load-on-startup>
	  </servlet>
	
	  <!-- servlet映射 -->
	  <servlet-mapping>
	  	<!-- 所有.do结尾的请求都由commServlet管理  -->
	  	<servlet-name>commServlet</servlet-name>
	  	<url-pattern>*.do</url-pattern>
	  </servlet-mapping>
	
	  <!-- 配置编码过滤器 -->
	  <filter>
	  	<filter-name>encodingFilter</filter-name>
	  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	  	<!-- 初始化参数,编码是UTF-8 -->
	  	<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>
	
	  <!-- 登录过滤 -->
	  <filter>
	    <filter-name>loginFilter</filter-name>
	    <filter-class>com.cjh.filter.LoginFilter</filter-class>
	  </filter>
	  
	  <!-- 登录过滤映射 -->
	  <filter-mapping>
<?xml version="1.0" encoding="UTF-8" ?>



Spzzing MVC:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	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   
          http://www.springframework.org/schema/util    
          http://www.springframework.org/schema/util/spring-util-3.0.xsd">


	<!-- ==================主键扫描,配置要扫描的Java类============================ -->
	<context:component-scan base-package="com.cjh" />
	
	
	<!-- ==================启动Spring MVC的注解功能,完成请求和注解POJO的映射,配置一个基于注解的定制的WebBindingInitializer,
	解决日期转换问题,方法级别的处理器映射============================ -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	    <property name="cacheSeconds" value="0" />
	    <!-- 配置一下对json数据的转换 -->
	    <property name="messageConverters">
	    	<list>
	    		<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
	    	</list>
	    </property>
	</bean>
	
	
	<!-- ==================定义一个视图解析类,基于ResourceView的解析器 ,此解析器是在url解析器上,
	加入了对jstl的支持============================ -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<!-- <property name="prefix" value="/jsp/" /> -->
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />

	</bean>
	

	<!-- ================== 处理文件上传 ============================ -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 默认编码 (ISO-8859-1) -->
		<property name="defaultEncoding" value="utf-8" />
		<!-- 最大内存大小 (10240)-->
		<property name="maxInMemorySize" value="10240" />
		<!-- 上传后的临时目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
		<property name="uploadTempDir" value="/upload_temp/" />
		<!-- 最大文件大小,-1为无限止(-1),注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和  -->
		<property name="maxUploadSize" value="-1" />
		
	</bean>


	<!-- ================== SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException
	该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 ============================ -->
	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到manage/view/fileError.jsp页面 -->
				<prop
					key="org.springframework.web.multipart.MaxUploadSizeExceededException">
					manage/view/fileError
				</prop>
			</props>
		</property>
	</bean>

</beans>

Spring:

<?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-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/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">
                     
                     
	<!-- ===================使用 annotation============================== -->
	<context:annotation-config />


	<!-- ===================hibernate属性配置============================ -->
	<context:property-placeholder location="classpath:hibernate/hibernate.properties" />


	<!-- ===================C3P0 数据源配置=============================== -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	    <!-- 驱动 -->
		<property name="driverClass" value="${c3p0.driver_class}" />
		<!-- 数据库连接url -->
		<property name="jdbcUrl" value="${c3p0.url}" />
		<!-- 用户名 -->
		<property name="user" value="${c3p0.username}"/>
		<!-- 密码 -->
		<property name="password" value="${c3p0.password}"/>
		<!-- 设置数据库连接池的最大连接数 -->  
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>  
        <!-- 设置数据库连接池的最小连接数 -->  
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>  
        <!-- 设置数据库连接池的初始化连接数 -->  
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>  
        <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->  
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>  
	</bean>


	<!-- ==================SessionFactory配置============================ -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<!-- 配置dataSource属性 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 扫描的包 -->
		<property name="packagesToScan" value="com.cjh.model" />
		<!-- 配置hibernate属性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					${hibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">
					${hibernate.show_sql}
				</prop>
				<prop key="hibernate.format_sql">
					${hibernate.format_sql}
				</prop>
				<prop key="hibernate.hbm2ddl.auto">
					${hibernate.hbm2ddl.auto}
				</prop>
				<prop key="hibernate.useUnicode">
					${hibernate.useUnicode}
				</prop>
				<prop key="hibernate.characterEncoding">
					${hibernate.characterEncoding}
				</prop>

			</props>
		</property>
	</bean>
	
	
	<!-- ==================配置hibernateTemplate========================= 
	<bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>-->
	
	
	<!-- ==================配置事务管理==================================== -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	
	<!-- ==================配置注解实现管理事务(cglib:proxy-target-class="true")================= -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />


</beans>

hibernate.priperties:

## C3P0配置
c3p0.driver_class=com.mysql.jdbc.Driver
c3p0.url=jdbc:mysql://127.0.0.1:3306/my
c3p0.username=root
c3p0.password=root
c3p0.maxPoolSize=100
c3p0.minPoolSize=3
c3p0.initialPoolSize=3
c3p0.maxIdleTime=100

## hibernate属性配置
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
hibernate.useUnicode=true
hibernate.characterEncoding=utf-8
 
## hibernate缓存配置
#hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
#hibernate.cache.use_query_cache=false
#hibernate.cache.use_second_level_cache=true

BaseDao代码:

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * 公共类
 * 
 * @author Only
 * 
 */
public class BaseDao {

	@Resource
	private SessionFactory sessionFactory;

	/**
	 * 获得session
	 * @return
	 */
	public Session getCurrentSession() {
		return sessionFactory.openSession();
	}

	/**
	 * 关闭session
	 * @param session
	 */
	public void closeSession(Session session) {
		try {
			if (null != session) {
				session.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}