文章插圖

文章插圖
SSH不是一個框架 , 而是多個框架(struts+spring+hibernate)的集成 , 是目前較流行的一種Web應用程序開源集成框架 , 用于構建靈活、易于擴展的多層Web應用程序 。
集成SSH框架的系統從職責上分為四層:表示層、業務邏輯層、數據持久層和域模塊層 , 以幫助開發人員在短期內搭建結構清晰、可復用性好、維護方便的Web應用程序 。其中使用Struts作為系統的整體基礎架構 , 負責MVC的分離 , 在Struts框架的模型部分 , 控制業務跳轉 , 利用Hibernate框架對持久層提供支持 , Spring做管理 , 管理struts和hibernate 。
SSH框架的系統是基于MVC的 。Struts 是一個很好的MVC框架 , 主要技術是Servlet和Jsp 。Struts的MVC設計模式可以使我們的邏輯變得很清晰 , 讓我們寫的程序層次分明 ?;赟truts開發可以簡化開發難度 , 提高開發效率 。
Spring 提供了管理業務對象的一致方法 , 并鼓勵注入對接口編程而不是對類編程的良好習慣 , 使我們的產品在最大程度上解耦 。
Hibernate 是用來持久化數據的 , 提供了完全面向對象的數據庫操作 。Hibernate對JDBC進行了非常輕量級的封裝 , 它使得與關系型數據庫打交道變得非常輕松 。
在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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/txhttp://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=http://www.mnbkw.com/jxjc/172518/”jdbc:mysql://localhost:3306/samblog?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true”>
</property>
<property name=”user” value=http://www.mnbkw.com/jxjc/172518/”root”>
<property name=”password” value=http://www.mnbkw.com/jxjc/172518/”123456″>
<property name=”driverClass” value=http://www.mnbkw.com/jxjc/172518/”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 Configuration2.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″?>
<!DOCTYPEhibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD3.0//EN””http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd”>
<!–
Mappingfile 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生成響應信息 , 執行后續攔截器處理
【ssh框架怎么運行 ssh框架工作流程】g.將響應信息輸出 。
- 饅頭蒸好后打開蓋就癟了是咋回事,蒸饅頭揭鍋蓋時饅頭縮進去怎么辦
- 安卓微信暗黑模式什么時候出,微信安卓深色模式怎么設置
- 圣羅蘭妍活青春粉底液怎么樣,圣羅蘭妍活青春粉底液色號選擇
- 軟件公司起什么名字好 起名軟件怎么樣
- 華為手機視頻轉換為mp4格式 華為手機視頻怎么轉成mp4格式
- 性格內向誠實人如何找到女朋友,感覺自己沒女朋友很丟人怎么辦
- 怎么和喜歡的男生在一起 怎么和暗戀的男生走到一起
- 一個女人對男人的曖昧 女人對曖昧對象怎么處理關系
- 分手了如何短時間內復合男友 怎么和直男男朋友復合
- 家里蟑螂多怎么辦 居家消滅蟑螂有妙招
