本篇文章給大家分享的是有關(guān)如何進行ssh整合分解,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、寧武ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的寧武網(wǎng)站制作公司
一、 Spring+Hibernate整合:
Spring整合Hibernate,是做了一個很大的調(diào)整的,因為spring可以把管理Hibernate的工作都做了,以前的hibernate.cfg.xml文件都去掉了,而將這些內(nèi)容都交給了spring來管理了。
1、 applicationContext.xml文件中應(yīng)該配置如下內(nèi)容:
Java代碼
//配置數(shù)據(jù)連接類 <bean id=“dataSource” class=“org.apache.commons.dbcp.BasicDataSource”> <property name=“driverClassName” value=“org.gjt.mm.MySQL.Driver”> </property> <property name=“url” value=“jdbc:mysql://localhost:3306/test”></property> <property name=“username” value=“root”></property> </bean> //配置session工廠類 <bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> <property name=“dataSource”> <ref bean=“dataSource” /> </property> <property name=“hibernateProperties”> <props> <prop key=“hibernate.dialect”> org.hibernate.dialect.MySQLDialect </prop> <prop key=“hibernate.show_sql”>true</prop> </props> </property> <property name=“mappingResources”> <value>com/hejianjiao/vo/Person.hbm.xml</value> </property> </bean> //配置數(shù)據(jù)連接類 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> </bean> //配置session工廠類 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <value>com/hejianjiao/vo/Person.hbm.xml</value> </property> </bean>
2、可以使用spring中的HibernateDAOSupport與HibernateTemplate類來進行數(shù)據(jù)持久化操作:
A、HibernateDAOSupport類中定義了對session、sessionFactory的操作方法與getHibernateTemplate方法來獲得一個HibernateTemplate實例;
B、HibernateTemplate類中定義了對數(shù)據(jù)持久化的各種封裝的方法,我們可以用它來對數(shù)據(jù)進行操作。
因此在使用時,我們可以繼承HibernateDAOSupport類,然后實例化HibernateTemplate類來進行數(shù)據(jù)持久化。
二、 Spring+Struts2整合:
1、spring配置在web.xml文件中的上下文監(jiān)聽器:
Java代碼
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframwork.web.content.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframwork.web.content.ContextLoaderListener</listener-class> </listener>2、struts2配置在web.xml文件中的過濾器: Java代碼 <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-patter>/*</url-patter> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-patter>/*</url-patter> </filter-mapping>
3、設(shè)置struts.xml文件,就可以使用spring的IOC來管理struts的Action:
Java代碼
<content name=“struts.objectFactory” value=“spring” > <content name="struts.objectFactory" value="spring" >
4、第三步設(shè)置后,以后在struts.xml 文件中配置一個action時,它的class就不是一個類了,而是在applicationContext.xml文件中定義過的類的ID,在 struts.xml文件中就只需要引用定義好的類的id 就可以了。
然后特別要注意的一個問題:action是一個請求就是一個action對 象,而在spring中則不是的,它是自動分配類的實例的,是使用的單 態(tài)模式來生產(chǎn)類的實例的,不符合action,因此在applicationContext.xml文件中定義每個action時,都要在類后加上:
Java代碼
scope=“prototype” 屬性
scope="prototype" 屬性三、 三者組合開發(fā):
一般在組合開發(fā)時,沒有什么難的,只要把上面兩步做好就可以是三個組合開發(fā)了。 上圖則是對于進行組合開發(fā)時,一般使用的系統(tǒng)架構(gòu):
1、 先從***層開發(fā),先開發(fā)POJO類,和Hibernate映射文件。它相當于系統(tǒng)的數(shù)據(jù)庫層。
2、 再開發(fā)DAO層,它是對于數(shù)據(jù)進行持久化的一層,專門處理各種數(shù)據(jù)增、刪、改、查的功能。并且使用DAO工廠模式,以保證和上層沒有任何的聯(lián)系,并且可以方便于類與接口的擴展。
3、 第三是開發(fā)manager層,它相當于軟件的業(yè)務(wù)邏輯層,即專門處理各種業(yè)務(wù)邏輯。實現(xiàn)系統(tǒng)的業(yè)務(wù)處理功能。并且它隔離事務(wù),使與下層的數(shù)據(jù)持久和上層的數(shù)據(jù)操作沒有任何的聯(lián)系。
4、 Action層,也即軟件的表示層,處理action的接收與回復(fù)。各action由spring管理。
五、 組合開發(fā)中的一些問題:
1、 在組合開發(fā)中,常見的一個問題就是session的管理,當我們使用HibernateTemplate操作數(shù)據(jù)庫時,可以不對session進行顯示的操作,spring可以自動處理session的打開與關(guān)閉。
我們可以在web.xml文件中顯示的配置一個session管理的過濾器,它專門幫助我們關(guān)閉session:
Java代碼
<filter> <filter-name>lazyLoadingFilter</filter-name> <filter-class> org.springframwork.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>lazyLoadingFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> 注:它一定要在struts2的過濾器之前。因為web.xml文件的過濾器執(zhí)行是有順序的。而session一定在前面進行。 <filter> <filter-name>lazyLoadingFilter</filter-name> <filter-class> org.springframwork.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>lazyLoadingFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
注:它一定要在struts2的過濾器之前。因為web.xml文件的過濾器執(zhí)行是有順序的。而session一定在前面進行。它會在所有的action處理完了,頁面顯示完了,就會自動關(guān)閉session。
六、 spring事務(wù)處理
1、事務(wù)的處理也交給了spring來管理,要在applicationContext.xml文件中上配置事務(wù)管理類:
Java代碼
//實施事務(wù)管理的bean
<bean id=”transactionManager” class=”org.springframwork.orm.hibernate3.HibernateTransactionManager”> <property name=”sessionFactory”> <ref bean=”sessionFactory” /> </property> </bean> //實施事務(wù)管理的bean <bean id=”transactionManager” class=”org.springframwork.orm.hibernate3.HibernateTransactionManager”> <property name=”sessionFactory”> <ref bean=”sessionFactory” /> </property>
</bean>它是通過sessionFactory來管理,因此在傳進來一個sessionFactory來接管事務(wù)處理。
2、 聲明式事務(wù)處理:
在spring中對事務(wù)進行管理時,可以顯示地進行事務(wù)處理的定義:
Java代碼
<tx:advice id=”txAdvice” transaction-manager=”transactionManager”> <tx:attributes >
//propagation表示的是事務(wù)的傳播特性,使用required時,是當檢測到add開頭的方法時,就看此時有沒有開啟的事務(wù),如果有則將方法放進事務(wù)中去,如果沒有,則新建一個事務(wù)。然后將方法放進去。
<tx:method name=”add*” propagation=”REQUIRED”> <tx:method name=”delete*” propagation=”REQUIRED”> <tx:method name=”update*” propagation=”REQUIRED”> //如果檢測到其它的方法,則給其只讀數(shù)據(jù)庫的屬性。即當本方法在讀時,其它的方法不能再去寫了。保證一個事務(wù)的完整性。 <tx:method name=”*” read-only=”true”> </tx:attributes> </tx:advice> //給事務(wù)添加的屬性 <tx:advice id=”txAdvice” transaction-manager=”transactionManager”> <tx:attributes >
//propagation表示的是事務(wù)的傳播特性,使用required時,是當檢測到add開頭的方法時,就看此時有沒有開啟的事務(wù),如果有則將方法放進事務(wù)中去,如果沒有,則新建一個事務(wù)。然后將方法放進去。
<tx:method name=”add*” propagation=”REQUIRED”>
<tx:method name=”delete*” propagation=”REQUIRED”>
<tx:method name=”update*” propagation=”REQUIRED”>
//如果檢測到其它的方法,則給其只讀數(shù)據(jù)庫的屬性。即當本方法在讀時,其它的方法不能再去寫了。保證一個事務(wù)的完整性。
<tx:method name=”*” read-only=”true”>
</tx:attributes>
</tx:advice>對于事務(wù)的其它傳播屬性,則可以參考其它文檔進行相關(guān)的了解。
上 一個配置是針對于所有包中類的事務(wù)處理方法的設(shè)置。下面一段是<aop:config/> 的定義,它確保由 ‘txAdvice’ bean定義的事務(wù)通知在應(yīng)用中合適的點被執(zhí)行。首先我們定義了 一個切面,它匹配 HibernateDAO 接口定義的所有操作,我們把該切面叫做 ‘allManagerMethod’。然后我們用一個通知器(advisor)把這個切面與 ‘txAdvice’ 綁定在一起,表示當 ‘allManagerMethod’ 執(zhí)行時,’txAdvice’ 定義的通知事務(wù)邏輯將被執(zhí)行。這就是AOP切面工程:
Java代碼
Java代碼 <aop:config> <aop:pointcut id=”allManagerMethod” expression=”execution(* com.hejianjiao.hibernate.HibernateDAO.*(..))”/> //調(diào)用上面配置的事務(wù)屬性,可以將它給本aop pointcut。 <aop:advisor advice-ref=”txAdvice” pointcut-ref=”allManagerMethod”/> //如果還有其它的定義,則可以再加上pointcut、advisor來定義本切面點的事務(wù)邏輯。 </aop:config> <aop:config> <aop:pointcut id=”allManagerMethod” expression=”execution(* com.hejianjiao.hibernate.HibernateDAO.*(..))”/> //調(diào)用上面配置的事務(wù)屬性,可以將它給本aop pointcut。 <aop:advisor advice-ref=”txAdvice” pointcut-ref=”allManagerMethod”/> //如果還有其它的定義,則可以再加上pointcut、advisor來定義本切面點的事務(wù)邏輯。
</aop:config> //expression中的內(nèi)容是要執(zhí)行本切面的一個接口,中的所有方法:如:一個接口中定義了操作數(shù)據(jù)的方 法:com.hejianjiao.hibernate.HibernateDAO,則下面execution括號中的內(nèi)容就為:* com.hejianjiao.hibernate.HibernateDAO.*(..)。而如果在com.hejianjiao.hibernate 包中還有其它的類也有操作方法,我們要一起定義的話,就可以寫為:* com.hejianjiao.*.*(..),其中(..)表示的是方法,前面的第一個是操作的接口或者類。
上面的配置將為由 ‘HibernateDAO’ 定義的bean創(chuàng)建一個代理對象,這個代理對象被裝配了事務(wù)通知,所以當它的相應(yīng)方法被調(diào)用時,一個事務(wù)將被啟動、掛起、被標記為只讀,或者其它(根據(jù)該方法所配置的事務(wù)語義)
以上就是如何進行ssh整合分解,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當前文章:如何進行ssh整合分解
當前路徑:http://chinadenli.net/article34/jgjgse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、用戶體驗、App設(shè)計、網(wǎng)站排名、手機網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)