Hibernate4 Struts2 Spring3整合
<!--版本采用hibernate-release-4.0.0.CR5 spring3.1.0 RC1 struts-2.2.3.1-->
jdbc.properties 放在src下
<!--JDBC资源配置文件-->
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/webdb
jdbc.username=webdb_admin
jdbc.password=123456
c3p0.acquireIncrement=10
c3p0.initialPoolSize=15
c3p0.minPoolSize=30
c3p0.maxPoolSize=120
c3p0.maxIdleTime=30
c3p0.idleConnectionTestPeriod=30
c3p0.maxStatements=300
c3p0.numHelperThreads=100
c3p0.checkoutTimeout=0
c3p0.validate=true
<!--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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/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/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName" default-lazy-init="true">
<!--加载JDBC资源(根据实际情况加载具体的jdbc.properties)-->
<util:properties id="jdbcProperties" location="classpath:jdbc.properties"/>
<!-- 数据源定义,使用c3p0 连接池 -->
<bean id="dataSource"
ref="dataSource" />
<property name="annotatedClasses">
<list><!--配实体层,也可定义个文件 把相应实体层放到相应文件中--></list>
</property>
<!--配置Hibernate相关信息-->
<property name="properties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.release_mode=auto
hibernate.connection.isolation=1
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=25
</value>
</property>
</bean>
<!--Hibernate事务管理(3)-->
<bean id="transactionManager"
/>
</bean>
<!--配置Spring AOP -->
<tx:advice id="transacAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*list" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.hanhn"/>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:annotation-config />
<aop:config>
<aop:pointcut id="executOption"
expression="execution(public * com.hanhn.*.*.*(..))" />
<aop:advisor advice-ref="transacAdvice" pointcut-ref="executOption" />
</aop:config>
</beans>
<!--Web.xml配置-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>webdb</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>
<!--初始化加载applationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<!--配置spring 监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Servlet 2.4+ listener that exposes the request to the current thread, through both LocaleContextHolder and RequestContextHolder-->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<!-- Listener that flushes the JDK's JavaBeans Introspector cache on web app shutdown -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--配置Struts2的过滤器-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!--释放资源-->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name> struts2 </filter-name>
<url-pattern>/*</url-pattern>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--过滤编码-->
<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>
<!--Servlet 2.3 Filter that binds a Hibernate Session to the thread for the entire processing of the request-->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
</web-app>
jar包地址:http://www.kuaipan.cn/index.php?ac=file&oid=25394217475907681 1 楼 panpan_xin 2012-01-12 hibernate4哪有这个类org.hibernate.cache.EhCacheProvider 啊?你配置缓存的时候就配置它 2 楼 lht0211 2012-02-24 怎么访问数据库的啊?