對Struts、spring、hibernate大體上了解一遍後,就是針對這個幾個框架的整合了。如何整合,請看下面:
第一:Struts2的jar和xml配置文件:
jar包:
commons-fileupload-1.2.1.jar:文件上傳
commons-io-1.3.2.jar:文件讀取工具類
freemarker-2.3.15.jar:模板引擎,基於模板生成文本輸出的通用工具。
ognl-2.7.3.jar:功能強大的表達式語言,替代EL表達式,進行數據綁定和顯示
struts2-core-2.1.8.1.jar:struts2核心包
xwork-core-2.1.6.jar:xwork核心包,是struts2的底層核心
xml文件有:web.xml (配置struts2的核心過濾器)
struts.xml (配置資源訪問)
第二:Spring的jar和xml配置文件
jar包:
spring.jar:包含有完整發布模塊的單個jar 包。但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2.jar
commons-logging:針對日誌處理的
aspectjrt:支持aop的jar
cglib-nodep-2.1_3:配合支持aop的jar
aspectjweaver.jsr 和 aspectjrt.jar:springAOP需要的包
xml文件有:applicationContext.xml
第三:Hibernate的jar和xml配置文件
jar包:
Hibernate3.jar:Hibernate的核心庫
antlr-2.7.6.jar:執行HQL語句的支持包
cglib-asm.jar:CGLIB庫,hibernate用它來實現PO字節碼的動態生成
dom4j.jar: dom4j:Java的XML API
commons-collections.jar: Apache Commons包中的一個,包含了一些Apache開發的集合類,功能比java.util.*強大
commons-logging.jar: Apache Commons包中的一個,包含了日誌功能
c3p0.jar: C3PO是一個數據庫連接池,Hibernate可以配置爲使用C3PO連接池。
jta.jar: JTA規範,當Hibernate使用JTA的時候需要
mysql-connector-java-5.1.5-bin.jar:鏈接mySql必須得包
xml文件有:hibernate.cfg.xml :針對每個實體持久化所做的配置,數據庫連接用戶名密碼等等。
xx.hbm.xml:每個實體對應一個
第四:Spring和Struts2、Spring和Hibernate整合時的XML配置和相關jar包
jar包:
Struts2和Spring整合時的jar:struts2-spring-plugin-2.1.8.1.jar是strus2和spring的一個整合插件。
Spring和Hibernate整合時不需要額外的jar包
xml配置:
1)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">
<!-- 配置spring的用於初始化容器對象的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext*.xml
</param-value>
</context-param>
<!-- ~~~~~~~~~~~struts2的配置 start~~~~~~~~~~~ -->
<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>
<!-- ~~~~~~~~~~~struts2的配置 end~~~~~~~~~~~ -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 設置爲開發模式 -->
<constant name="struts.devMode" value="true" />
<!-- 擴展名配置爲action -->
<constant name="struts.action.extension" value="action"/>
<!-- 主題,將值設置爲simple,即不使用UI模板。這將不會生成額外的html標籤 -->
<constant name="struts.ui.theme" value="simple" />
</struts>
3)Spring的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"
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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--導入外部properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 連接池 -->
<property name="dataSource" >
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--mysql數據庫驅動 -->
<property name="driverClass" value="${driverClass}"></property>
<!-- mysql數據庫名稱 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<!-- 數據庫的登陸用戶名 -->
<property name="user" value="${user}"></property>
<!-- 數據庫的登陸密碼 -->
<property name="password" value="${password}"></property>
<!-- 方言:爲每一種數據庫提供適配器,方便轉換 -->
<!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
<!-- 其他配置 -->
<!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--連接池中保留的最小連接數。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--連接池中保留的最大連接數。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制數據源內加載的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空閒時間,1800秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>
</beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<!-- 方言:爲每一種數據庫提供適配器,方便轉換 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
xx.hbm.xml配置,就不用說了。
總結:
就這樣,SSH框架整合完成了,通過這些配置理解這個框架的設計和思想,這樣纔是最好的。SSH框架整合好了,就通過一個實例檢測下吧。見下篇博客。