詳解SSH 框架中對象調用流程

摘要:SSH=Struts+Spring+Hibernate

SSH不是一個框架,而是多個框架(struts+spring+hibernate)的集成,是目前較流行的一種Web應用程序開源集成框架,用於構建靈活、易於擴展的多層Web應用程序。

集成SSH框架的系統從職責上分爲四層:表示層、業務邏輯層、數據持久層和域模塊層,以幫助開發人員在短期內搭建結構清晰、可複用性好、維護方便的Web應用程序。其中使用Struts作爲系統的整體基礎架構,負責MVC的分離,在Struts框架的模型部分,控制業務跳轉,利用Hibernate框架對持久層提供支持,Spring做管理,管理struts和hibernate。

SSH框架的系統是基於MVC的。Struts 是一個很好的MVC框架,主要技術是Servlet和Jsp。Struts的MVC設計模式可以使我們的邏輯變得很清晰,讓我們寫的程序層次分明。基於Struts開發可以簡化開發難度,提高開發效率。

Spring 提供了管理業務對象的一致方法,並鼓勵注入對接口編程而不是對類編程的良好習慣,使我們的產品在最大程度上解耦。

Hibernate 是用來持久化數據的,提供了完全面向對象的數據庫操作。Hibernate對JDBC進行了非常輕量級的封裝,它使得與關係型數據庫打交道變得非常輕鬆。

在Struts+Spring+Hibernate系統中,對象之間的調用流程如下:

 Struts——>Spring——>Hibernate

 JSP——>Action——>Service——>DAO——>Hibernate

比如:

1.Spring的配置文件bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
        xmlns:tx="http://www.springframework.org/schema/tx">


    <bean id="dataSource"
          class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="jdbcUrl"
                  value="jdbc:mysql://localhost:3306/samblog?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true">
        </property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
    </bean>
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=false
                hibernate.format_sql=false
            </value>
        </property>
        <property name="mappingResources">
            <list>
                <value>site/sambloger/domain/Users.hbm.xml</value>
                <value>site/sambloger/domain/Blog.hbm.xml</value>
                <value>site/sambloger/domain/Category.hbm.xml</value>
                <value>site/sambloger/domain/Comment.hbm.xml</value>
            </list>
        </property>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- 配置Blog  spring進行管理  服務層直接調用實現與數據庫的CRUD-->
    <bean id="blogDao" class="site.sambloger.dao.impl.BlogDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="blogService" class="site.sambloger.service.impl.BlogServiceImpl" scope="prototype">
        <property name="blogDao" ref="blogDao"/>
    </bean>
    <bean id="blogAction" class="site.sambloger.action.BlogAction">
        <property name="blogService" ref="blogService"/>
        <property name="commentService" ref="commentService"/>
    </bean>

    <!-- 配置Comment -->
    <bean id="commentDao" class="site.sambloger.dao.impl.CommentDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="commentService" class="site.sambloger.service.impl.CommentServiceImpl" scope="prototype">
        <property name="commentDao" ref="commentDao"/>
    </bean>
    <bean id="commentAction" class="site.sambloger.action.CommentAction">
        <property name="commentService" ref="commentService"/>
        <property name="blogService" ref="blogService"/>
    </bean>

    <!-- 配置Users -->
    <bean id="usersDao" class="site.sambloger.dao.impl.UsersDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="usersService" class="site.sambloger.service.impl.UsersServiceImpl" scope="prototype">
        <property name="usersDao" ref="usersDao"/>
    </bean>
    <bean id="usersAction" class="site.sambloger.action.UsersAction">
        <property name="userService" ref="usersService"></property>
    </bean>
</beans>

2.Struts的配置文件 struts.xml

<?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>
    <package name="samblog" extends="struts-default" namespace="/">
             <action name="init" class="blogAction" method="init">
                    <result name="success">/bloglist.jsp</result>
            </action>
            <action name="getBlog" class="blogAction" method="getBlog">
                    <result name="success">/displayBlog.jsp</result>
            </action> 
            <action name="getAllNote" class="blogAction" method="getAllNote">
                <result name="success">/notelist.jsp</result>
            </action>
            <action name="addComment" class="commentAction" method="addComment">
                <result name="success"  type="redirect">/getBlog</result>
            </action>
    </package>
</struts>    

3.Hibernate其中的一個配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools-->
<hibernate-mapping>
    <class name="site.sambloger.domain.Blog" table="blog">
        <!--id標籤表示映射到數據庫中是作爲主鍵 其他property表示普通鍵-->
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
<!--該標籤加N方 會有一個字段叫category_id作爲外鍵參照1(Category)的主鍵字段 並且用來存儲這個主鍵的信息-->
        <many-to-one name="category" class="site.sambloger.domain.Category"  lazy="false" cascade="all">
            <column name="category_id" not-null="true" />
        </many-to-one>

        <property name="title" type="java.lang.String">
            <column name="title" length="400" not-null="true" />
        </property>

        <property name="content" type="java.lang.String">
            <column name="content" length="4000" not-null="true" />
        </property>

        <property name="createdTime" type="java.util.Date">
            <column name="created_time" length="10" not-null="true" />
        </property>
<!--在一對多的關聯中,在一的一方(Blog)設置inverse=”true”讓多的一方來維護關聯關係更有助於優化,因爲可以減少執行update語句-->
        <set name="comments" inverse="true">
            <key>
                <column name="blog_id" not-null="true" />
            </key>
            <one-to-many class="site.sambloger.domain.Comment" />
        </set>
    </class>
</hibernate-mapping>

Spring框架的作用和好處:

Spring框架提供了一個容器,該容器可以管理應用程序的組件,還提供了IoC和AoP機制,實現組件之間解耦,提高程序結構的靈活性,增強系統的可維護和可擴展性。

在SSH整合開發中,利用Spring管理Service、DAO等組件,利用IoC機制實現Action和Service,Service和DAO之間低耦合調用。利用AoP機制實現事務管理、以及共通功能的切入等。

功能是整合,好處是解耦。

Hibernate中操作併發處理(樂觀鎖和悲觀鎖)

Hibernate框架可以使用鎖的機制來解決操作併發。

a.悲觀鎖

在數據查詢出來時,就給數據加一個鎖,鎖定。這樣其他用戶再執行刪、改操作時不允許。當佔用着事務結束,鎖會自動解除。

Hibernate採用的是數據庫鎖機制實現悲觀鎖控制。

缺點:將併發用戶操作同步開,一個一個處理。當一個用戶處理時間比較長時,效率會比較低。

b.樂觀鎖

允許同時更新提交,但是最快的會成功,慢的失敗。

在記錄中追加一個字段值,用該字段值當做版本。當最先提交者提交後,會自動將版本字段值提升,這樣其他用戶提交,會發現版本低於數據庫記錄目前版本,因此拋出異常提示失敗。

特點:允許用戶同時處理,但只能有一個成功,其他失敗,以異常方式提示。

SSH工作流程

a.啓動服務器,加載工程以及web.xml.

(實例化Lisener,Filter等組件,將Spring容器和Struts2控制創建)

b.客戶端發送請求,所有請求進入Struts2控制器。控制器根據請求類型不同,分別處理。

(action請求,*.action會進入struts.xml尋找<action>配置.

其他請求,*.jsp會直接調用請求資源,生成響應信息)

c.Struts2控制器根據<action>配置調用一個Action對象處理。

整合方法一:將Action交給Spring容器

(Action對象由struts2-spring-plugin.jar插件提供的

StrutsSpringObjectFactory負責去Spring容器獲取)

整合方法二:將Action置於Spring容器之外

(Action對象由struts2-spring-plugin.jar插件提供的

StrutsSpringObjectFactory負責創建,然後到Spring容器中尋找與Action屬性匹配的Bean對象,給Action對象注入。(默認採用名稱匹配規則)

d.Struts2控制器執行defaultStack攔截器、Action對象、Result等組件處理.

e.執行Action的execute業務方法時,如果使用Service或DAO採用Spring的IoC機制調用。

f.執行Result生成響應信息,執行後續攔截器處理

g.將響應信息輸出。

本文分享自華爲雲社區《SSH 組合框架模式小知識分享》,原文作者:Jack20 。

 

點擊關注,第一時間瞭解華爲雲新鮮技術~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章