ssh框架介紹

 

 

 

SSH 爲 struts+spring+hibernate 的一個集成框架,是目前較流行的一種JAVA Web應用程序開源框架。java

 Struts

  Struts是一個基於Sun J2EE平臺的MVC框架,主要是採用Servlet和JSP技術來實現的。因爲Struts能充分知足應用開發的需求,簡單易用,敏捷迅速,在過去的一年中頗受關注。Struts把Servlet、JSP、自定義標籤和信息資源(message resources)整合到一個統一的框架中,開發人員利用其進行開發時不用再本身編碼實現全套MVC模式,極大的節省了時間,因此說Struts是一個很是不錯的應用框架。mysql

  官方地址:http://struts.apache.org程序員

 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

  官方地址:spring: http://www.springsource.orgsql

 Hibernate

  Hibernate是一個開放源代碼的對象關係映射框架,它對JDBC進行了很是輕量級的對象封裝,使得Java程序員能夠爲所欲爲的使用對象編程思惟來操縱數據庫。 Hibernate能夠應用在任何使用JDBC的場合,既能夠在Java的客戶端程序實用,也能夠在Servlet/JSP的Web應用中使用,最具革命意義的是,Hibernate能夠在應用EJB的J2EE架構中取代CMP,完成數據持久化的重任。數據庫

  官方地址:http://www.hibernate.orgapache

 在SSH 的組合框架模式中,三者各自的做用

  Struts 是一個很好的MVC框架,主要技術是Servlet和Jsp。Struts的MVC設計模式可使咱們的邏輯變得很清晰,讓咱們寫的程序井井有條。編程

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

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

  如下是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&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>
相關文章
相關標籤/搜索