下面給出整合開發時Struts 2、 Hibernate、Spring需要的JAR,包太多不打字了,直接截圖。
ps:我資料庫用的是mysql

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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
第一步在Spring中配置資料源:
<!-- 配置資料源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/資料庫名字?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="登入mysql的使用者名" />
<property name="password" value="登入密碼" />
<!--初始化時擷取的連接配接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!--連接配接池中保留的最小連接配接數。 -->
<property name="minPoolSize" value="1" />
<!--連接配接池中保留的最大連接配接數。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!--最大空閑時間,60秒内未使用則連接配接被丢棄。若為0則永不丢棄。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--當連接配接池中的連接配接耗盡的時候c3p0一次同時擷取的連接配接數。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--每60秒檢查所有連接配接池中的空閑連接配接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
第二步內建進hibernate:
<!-- 配置會話工廠() -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 設定資料源 -->
<property name="dataSource" ref="dataSource" />
<!-- 接管了hibernate對象映射檔案 -->
<property name="mappingResources">
<list>
<value>com/lmk/domain/City.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
<!-- 啟動二級緩存,我這裡用了EhCache的方式,需要加ehcache.xml檔案 -->
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.generate_statistics=true
</value>
</property>
</bean>
第三步使用Spring容器管理事務服務:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--使用基于注解方式配置事務 -->
<tx:annotation-driven transaction-manager="txManager"/>
第四部 配置 hibernate 實體映射檔案City.hbm.xml用MyEclipse Hibernate的工具即可(前提是先建立好資料庫)。
第五步 在web容器中使用Listener執行個體化spring容器和配置struts2
<!-- 對Spring容器進行執行個體化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置struct2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 自從Struts 2.1.3以後,下面的FilterDispatcher已經标注為過時 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第六步 struts2的配置檔案模版struts.xml如下。常量struts.objectFactory=spring明确指出将由Spring負責建立Action實
例。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 預設的視圖主題 -->
<constant name="struts.ui.theme" value="simple" />
<!-- spring管理action -->
<constant name="struts.objectFactory" value="spring" />
<package name="person" namespace="/person" extends="struts-default">
<global-results>
<result name="message">/WEB-INF/page/message.jsp</result>
</global-results>
<action name="action_*" class="personList" method="{1}">
<result name="list">/WEB-INF/page/persons.jsp</result> </action>
</package>
</struts>
為了能從spring容器中尋找到Action bean,要求action配置的class屬性值和spring中bean的名稱相同。
第七部 使用spring解決struts2亂碼問題,在web.xml檔案中配置。
<!-- 使用spring解決亂碼問題 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第八步 使用spring解決hibernate因session關閉導緻的延遲加載例外問題,在web.xml檔案中配置。
<!-- 使用spring解決hibernate的懶加載 要在struct2的配置前面 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<!-- <init-param> 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置檔案中的名稱,
預設值為sessionFactory.如果LocalSessionFactoryBean在spring中的名稱不是sessionFactory,
該參數一定要指定,否則會出現找不到sessionFactory的例外 <param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value> </init-param> -->
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
前期配置工作大概就這麼多啦,又完成一篇筆記!