SSH 為 struts+spring+hibernate 的一個集成框架,是目前較流行的一種JAVA Web應用程序開源框架。
Struts
Struts是一個基於Sun J2EE平台的MVC框架,主要是采用Servlet和JSP技術來實現的。由於Struts能充分滿足應用開發的需求,簡單易用,敏捷迅速,在過去的一年中頗受關注。Struts把Servlet、JSP、自定義標簽和信息資源(message resources)整合到一個統一的框架中,開發人員利用其進行開發時不用再自己編碼實現全套MVC模式,極大的節省了時間,所以說Struts是一個非常不錯的應用框架。
Spring
Spring是一個解決了許多在J2EE開發中常見的問題的強大框架。 Spring提供了管理業務對象的一致方法並且鼓勵了注入對接口編程而不是對類編程的良好習慣。Spring的架構基礎是基於使用JavaBean屬性的Inversion of Control容器。然而,這僅僅是完整圖景中的一部分:Spring在使用IOC容器作為構建完關注所有架構層的完整解決方案方面是獨一無二的。 Spring提供了唯一的數據訪問抽象,包括簡單和有效率的JDBC框架,極大的改進了效率並且減少了可能的錯誤。Spring的數據訪問架構還集成了Hibernate和其他O/R mapping解決方案。Spring還提供了唯一的事務管理抽象,它能夠在各種底層事務管理技術,例如JTA或者JDBC事務提供一個一致的編程模型。Spring提供了一個用標准Java語言編寫的AOP框架,它給POJOs提供了聲明式的事務管理和其他企業事務--如果你需要--還能實現你自己的aspects。這個框架足夠強大,使得應用程序能夠拋開EJB的復雜性,同時享受着和傳統EJB相關的關鍵服務。Spring還提供了可以和IoC容器集成的強大而靈活的MVC Web框架。
官方地址:spring: http://www.springsource.org
Hibernate
Hibernate是一個開放源代碼的對象關系映射框架,它對JDBC進行了非常輕量級的對象封裝,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程序實用,也可以在Servlet/JSP的Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成數據持久化的重任。
在SSH 的組合框架模式中,三者各自的作用
Struts 是一個很好的MVC框架,主要技術是Servlet和Jsp。Struts的MVC設計模式可以使我們的邏輯變得很清晰,讓我們寫的程序層次分明。
Spring 提供了管理業務對象的一致方法,並鼓勵注入對接口編程而不是對類編程的良好習慣,使我們的產品在最大程度上解耦。
Hibernate 是用來持久化數據的,提供了完全面向對象的數據庫操作。Hibernate對JDBC進行了非常輕量級的封裝,它使得與關系型數據庫打交道變得非常輕松。
以下是SSH架構圖:
SSH架構圖
Struts負責Web層:
ActionFormBean接收網頁中表單提交的數據,然后通過Action進行處理,再Forward到對應的網頁,在Struts-config.xml中定義了<action-mapping>,ActionServlet會加載進來。
Spring負責業務層管理,即Service:
Service為Action提供統一的調用接口,封裝持久層的DAO,並集成Hibernate,Spring可對JavaBean和事物進行統一管理。
Hibernate負責持久層,完成數據庫的CRUD操作:
Hibernate有一組hbm.xml文件和PO,是與數據庫中的表相對應的,然后定義DAO,這些是與數據庫打交道的類。
在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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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&characterEncoding=UTF-8&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>