struts2 hibrnate4.1. spring3.1整合

2 篇文章 0 订阅

我用以前的hibernate3的方式整合了ssh但会报错经过努力的查找资料终于把这个问题解决了

如果使用hibernate4的jar包使用hibernate3的方式配置会提示 java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider找不到类CacheProvider,是因为hibernate3是就不推荐使用这个缓存机制,然而到了hibernate4时在hibernate核心包中删除了这个类所以找不到,所以就创建不了sessionFactory。

要是配置好就要使用hibernate4提供的方法配置sessionFactory的class是org.springframework.orm.hibernate4.LocalSessionFactoryBean,以前听说LocalSessionFactoryBean不支持注解,但是hibernate4经过试验是支持注解的这一点要变还有一点是二级缓存配置要变根据hibernate4的推荐配置是

<prop key="hibernate.cache.region.factory_class">
org.hibernate.cache.EhCacheRegionFactory
</prop>

speing的事务管理要改成hibernate4的

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


二级缓存配置文件加载可以还是老样子

<prop key="hibernate.cache.provider_configuration_file_resource_path">

ehcache.xml
</prop>

还可以是改成hibernate4推荐的

<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>


其他都还一样

spring使用注解方式hibernate使用注解方式struts使用注解方式只需要一个applicationContext.xml和一个ehcache.xml

其中applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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/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">
	
	<!-- 启动自动扫描使用注解方式管理bean的方式打开否则去掉 -->
	<context:component-scan base-package="com.kaishengit" />
	<!--启动注解方式事务管理否则使用   <tx:advice/>  -->
	<tx:annotation-driven transaction-manager="transactionManager" />

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 启动自动扫描pojo 使用注解方式配置pojo使用否则去掉 -->
		<property name="packagesToScan" value="com.kaishengit.pojo"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<!-- 缓存 -->
				<prop key="hibernate.cache.region.factory_class">
					org.hibernate.cache.EhCacheRegionFactory
				</prop>
				<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
			</props>
		</property>
	</bean>



	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql:///product" />
		<property name="maxIdleTime" value="25000" />
		<property name="properties">
			<props>
				<prop key="user">root</prop>
				<prop key="password">root</prop>
				<prop key="c3p0.max_size">20</prop>
				<prop key="c3p0.min_size">5</prop>
			</props>
		</property>
	</bean>
</beans>



另外web.xml也要更改一下把opensessioninviwe改成hibernate4的不然访问网页会报错

<?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">
	<display-name></display-name>
	
  <filter>
    <filter-name>openSessionInView</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  
  
  <filter-mapping>
    <filter-name>openSessionInView</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<listener> 
		<listener-class> 
		  org.springframework.web.context.ContextLoaderListener 
		</listener-class> 
 	</listener>
  
  <context-param> 
	<param-name>contextConfigLocation</param-name> 
	<param-value>  
	    classpath:applicationContext*.xml
	</param-value> 
  </context-param> 

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值