SSH的簡單入門體驗(Struts2.1+Spring3.1+Hibernate4.1)- 查詢系統(上)

所謂SSH,指的是struts+spring+hibernate的一個集成框架,它是目前較流行的一種Web應用程序的開源框架。mysql

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

畢業實習佈置了一個利用SSH框架作一個查詢系統的做業,結果跟駕校科目三的考試衝突了,很悲劇的沒有遇上第一次的檢查做業,只能等國慶後再去補交了。我從SSH框架的佈置到系統的簡單實現一共花了三天的時間,裏面的概念還不是很明白,只是知道怎麼作而已。寫下這篇文章也是爲了之後從新入門SSH的時候有個入門介紹,防止之後還要從頭開始學起(雖然以爲之後若是要寫SSH的項目的話確定要從新系統的學習一遍,無視無視~)web

 

1、準備工具

  1. MyEclipse2014GA版本
  2. MySql5.6 和 其驅動包
  3. Tomcat 8

我這裏提供一下MyEclipse的下載連接吧,正好網盤裏有(附帶破解),其餘的工具請你們自行斟酌,由於MyEclipse已經自帶數據庫和Tomcat了,你們可使用自帶的工具。我這裏是使用了上述的工具。spring

MyEclipse下載連接:百度網盤sql

MyEclipse破解文件下載連接:百度網盤數據庫

2、新建Web項目,導入SSH

  1. 首先第一步是新建一個web project的項目

    點擊next,下一步

    點擊finish,完成web project的建立。
  2. 接下來咱們導入SSH框架所需的文件
    首先在CnDemo項目根文件夾上右鍵

    我沒記錯的話myeclipse早幾期的版本菜單是add xxxx的樣式,你們對號入座就好了。
  3. 首先是安裝spring框架,選擇Install Spring Facet.

    點擊finish,這樣spring3.1框架就導入進去了。
  4. 添加Hibernate,不過在此以前你要先創建數據庫的連接

    這是個人數據庫鏈接,使用了mysql。在Connection Url這裏要填寫你要鏈接的域名和數據庫名以及用戶名和密碼。要導入對應的數據庫驅動jar文件才行。最後不要忘記測試一下數據庫是否能用。
  5. 導入Hibernate框架,選擇Install Hibernate Facet.


    點擊完成,建立完畢。
  6. 最後是導入struts框架,選擇Install Apache Struts(2.x) Facet.

    點擊完成就行了。

以上這樣配置,就算基本上把SSH最簡單的框架給搭建好了。apache

 

3、關於SSH的配置文件

其中最重要的是這兩個配置文件
spring配置文件:applicationContext.xml。裏面存放實體來控制整個系統。
struts配置文件:struts.xml。裏面存放action的映射。
session

  1. applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
        xmlns:tx="http://www.springframework.org/schema/tx">
    
        <!-- 數據庫實體 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver">
            </property>
            <property name="url" value="jdbc:mysql://localhost/querydb"></property>
            <property name="username" value="root"></property>
            <property name="password" value="shen"></property>
        </bean>
    
        <!-- hibernate實體 -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.MySQLDialect
                    </prop>
                    <!-- hibernate 數據的更新方式 -->
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <!-- 解決no session found -->
                    <!--使用getCurrentSession()須要加入以下配置 -->
                    <prop key="hibernate.current_session_context_class">thread</prop>
                    <!-- hibernate分頁出現 ResultSet may only be accessed in a forward direction須要設置hibernate結果集滾動 -->
                    <!-- <prop key="jdbc.use_scrollable_resultset">false</prop> -->
                </props>
    
            </property>
    
            <!-- 配置實體Hibernate的描述文件 -->
            <property name="mappingResources">
                <list>
                    <!-- 實體類列表 -->
                    <value>cpacm/pojo/Archive.hbm.xml</value>
                </list>
            </property>
        </bean>
    
        <!-- 控制層 struts實體(Action實體) -->
        <bean id="ArchiveAction" class="cpacm.action.ArchiveAction" scope="prototype">
            <property name="archiveService" ref="ArchiveService"></property>
        </bean>
    
        <!-- 邏輯層 service -->
        <bean id="ArchiveService" class="cpacm.service.ArchiveService">
            <property name="archiveDao" ref="ArchiveDao"></property>
        </bean>
    
        <!-- 數據層,用於數據庫的操做 -->
        <bean id="ArchiveDao" class="cpacm.dao.ArchiveDao">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
    
        <!-- spring自帶的事物管理 -->
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <bean id="txManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="create*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="findByQuery*" propagation="REQUIRED" />
                <tx:method name="*" read-only="true" />
            </tx:attributes>
        </tx:advice>
    
        <aop:config proxy-target-class="true">
            <aop:advisor advice-ref="txAdvice"
                pointcut="execution(* net.noday..service..*.*(..))" />
    
        </aop:config>
    
    
    </beans>

    其中,bean的注入基本方法爲
    架構


  2. 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>
        <!-- action由spring進行實例化 -->
        <constant name="struts.objectFactory" value="spring" />
    
        <constant name="struts.devMode" value="true"></constant>
    
        <package name="MySSH" extends="struts-default">
            <!-- <action name="test" class="cpacm.struts2.demoAction" method="execute"> 
                <result name="success">/Strut2Test.jsp</result> </action> -->
    
            <!-- 全局的通用的action -->
            <global-results>
                <result name="error">/error.jsp</result>
                <!-- <result name="success">/success.jsp</result> -->
            </global-results>
            
            <!-- Action列表,其中class=xxx爲spring中的id -->
            <action name="query" class="ArchiveAction" method="Query">
                <result name="Query">/frame/ArcDataGrid.jsp</result>
            </action>
            <action name="tag" class="ArchiveAction" method="toUpdate">
                <result name="toUpdate">/frame/ArcUpdate.jsp</result>
            </action>
            <action name="update" class="ArchiveAction" method="Update">
                <result name="success">/frame/ArcDataGrid.jsp</result>
            </action>
            <action name="delete" class="ArchiveAction" method="Delete">
                <result name="success">/frame/ArcDataGrid.jsp</result>
            </action>
            <action name="queryByclassId" class="ArchiveAction" method="QueryByclassID">
                <result name="success">/frame/ArcDataGrid.jsp</result>
            </action>
            <action name="ArcAdd" class="ArchiveAction" method="Add">
                <result name="success">/frame/ArcAddData.jsp</result>
            </action>
        </package>
    
    </struts>

以上基本就是配置的所有過程了,可能很簡單,但也是五臟俱全了。篇幅有限,接下來會在下一篇講述怎麼建一個完整的系統。app

相關文章
相關標籤/搜索