首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

SSH 整合實戰(一):Spring 4.2 + Hibernate 4.3 + Struts 2.3 逐步整合指南

     編輯:Java 相關知識

本文記錄一個網上商城專案從零開始搭建 SSH(Struts2 + Spring + Hibernate)整合環境的完整流程,採用「分層驗證、逐步整合」策略,確保每一步可獨立運行。

整合順序

  1. 搭建 Spring 環境 → 驗證 IoC
  2. 搭建 Hibernate 環境 → 驗證持久化
  3. 整合 Spring + Hibernate → 交由 Spring 管理 SessionFactory 與事務
  4. 搭建 Struts 2 環境 → 驗證 Action 流程
  5. 整合 Spring + Struts 2 → Action 交由 Spring 管理

1. Jar 包分類管理

建議在 IDE 的 User Library 中按功能分類:

  • spring-4.2.4-core:核心容器(spring-core, spring-beans, spring-context, spring-expression)
  • spring-4.2.4-aop:AOP 相關(spring-aop, aopalliance, aspectjweaver)
  • spring-4.2.4-persistence:持久化整合(spring-orm, spring-tx, spring-jdbc)
  • spring-4.2.4-web:Web 整合(spring-web, spring-webmvc, struts2-spring-plugin)
  • hibernate-4.3.11:Hibernate 核心與 JPA
  • c3p0-0.9.5.1:連線池
  • mysql-connector-java-5.1.26:驅動

2. Spring 環境搭建與 IoC 驗證

  • 建立 beans.xml,配置掃描或手動定義 Bean。
  • 編寫 JUnit 測試類,使用 @RunWith(SpringJUnit4ClassRunner.class) + @ContextConfiguration 載入配置,注入 java.util.Date 驗證 IoC 容器啟動正常。

3. Hibernate 環境搭建(MyEclipse 逆向工程)

  1. 建立資料庫 shop 與表 category。
  2. DB Browser 連接 MySQL,右鍵表選擇 Hibernate Reverse Engineering 生成 Model(Category.java)與映射檔(Category.hbm.xml)或註解。
  3. MyEclipse 自動生成 HibernateSessionFactory(單例 + ThreadLocal 模式)。
  4. 編寫 CategoryService 手動管理 Session 與事務,測試資料入庫成功。

4. Spring 整合 Hibernate:聲明式事務

beans.xml 關鍵配置

<!-- 資料源:交給 Spring 管理,hibernate.cfg.xml 可移除連線配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/>
</bean>

<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- AOP 事務通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="*" propagation="SUPPORTS"/>
    </tx:attributes>
</tx:advice>

<!-- 切面:service.impl 包下所有方法 -->
<aop:config>
    <aop:pointcut id="pc" expression="execution(* cn.it.shop.service.impl.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>

Service 層改造

  • 移除手動 Session/事務代碼,改用 sessionFactory.getCurrentSession() 獲取 Spring 綁定的 Session。
  • 在 beans.xml 註冊 Service Bean,注入 sessionFactory。

測試:JUnit 直接注入 CategoryService 呼叫 update(),資料庫更新成功,聲明式事務生效。

5. Struts 2 環境搭建

  • web.xml 配置 StrutsPrepareAndExecuteFilter,攔截 *.action。
  • 建立 CategoryAction 繼承 ActionSupport,struts.xml 使用動態方法調用 category_*{method}。
  • 驗證:JSP 連結觸發 Action,控制台輸出 null(尚未整合 Spring),流程通順。

6. Spring 整合 Struts 2

  1. 加入 struts2-spring-plugin-2.3.24.1.jar(位於 spring-web 分類)。
  2. beans.xml 定義 Action Bean(scope="prototype"),注入 Service:
<bean id="categoryAction" class="cn.it.shop.action.CategoryAction" scope="prototype">
    <property name="categoryService" ref="categoryService"/>
</bean>
  1. struts.xml 的 class 改為 Spring Bean ID:class="categoryAction"。
  2. web.xml 加入 ContextLoaderListener,指定 contextConfigLocation 為 classpath:beans.xml(Listener 需在 Filter 之前啟動)。

重新測試:控制台輸出 Service 實例與 SQL,Action 已由 Spring 管理,整合完成。

專案結構速覽

src
├─ cn.it.shop.action      // Struts2 Action
├─ cn.it.shop.service     // Service 介面
├─ cn.it.shop.service.impl// Service 實現(Spring 事務切面)
├─ cn.it.shop.dao         // DAO 介面(後續抽取)
├─ cn.it.shop.model       // Hibernate 實體
└─ cn.it.shop.util        // 基礎類
WebRoot
├─ WEB-INF
│   ├─ classes/beans.xml  // Spring 總配置
│   ├─ classes/hibernate.cfg.xml
│   ├─ struts.xml
│   └─ web.xml
└─ js/jsp...

至此,SSH 整合環境完全就緒,可進入業務開發階段。完整原始碼可參考文末下載連結。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved