struts+spring+hibernate感悟

來源:互聯網
上載者:User
struts+spring+hibernate感悟 三者的結合堪稱完美
整合三者關鍵是設定檔
1.web.xml 用於web伺服器裝載過濾器,servlet,以及設定檔
struts 在此被裝載org.apache.struts.action.ActionServlet,還有它的配置參數config檔案struts-config.xml,spring在此被裝載org.springframework.web.context.ContextLoaderServlet還有它的設定檔applicationContext.xml,其他省略不列出
例子:
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>  <servlet-name>action</servlet-name>
  <servlet-class>
   org.apache.struts.action.ActionServlet
  </servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>
   org.springframework.web.context.ContextLoaderServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <!-- Action Servlet Mapping -->
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
2.struts-config.xml這個檔案
這個是strust專有設定檔,配置form,全域轉寄,action,message-resources(用於顯示提示資訊),外掛程式
格式如下:
<struts-config>
 <form-beans>
  <form-bean name="logonform" type="com.binghe.forms.LogonForm" />
  .....
</form-beans>
<global-forwards>
  <forward name="logon" path="/login.jsp" />
  .......
</global-forwards>
<action-mappings>
  <action path="/logon"
   type="org.springframework.web.struts.DelegatingActionProxy" name="logonform"
   input="/login.jsp" scope="request" validate="true">
   <forward name="logon" path="/login.jsp" />
   .....
  </action>
  ......
</action-mappings>
<controller locale="true" />
<message-resources parameter="com.binghe.struts.ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
  <set-property property="pathnames"
   value="/WEB-INF/validator-rules.xml,
                            /WEB-INF/validation.xml" />
 </plug-in>
 <plug-in
  className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation"
   value="/WEB-INF/applicationContext.xml" />
 </plug-in>
</struts-config>
3.applicationContext.xml.xml 
這個是spring的專有設定檔,裡面配置代理hibernate資源和struts的 action
格式如下:
<beans>
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName">
   <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
  </property>   <property name="url">
   <value>jdbc:microsoft:sqlserver://127.0.0.1:1400;DatabaseName=books</value>
  </property>
  <property name="username">
   <value>sa</value>
  </property>
  <property name="password">
   <value>123</value>
  </property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <property name="mappingResources">
   <list>
    <value>
     com/binghe/hibernate/booktype/BookType.hbm.xml
    </value>
    <value>
     com/binghe/hibernate/book/Book.hbm.xml
    </value>
  </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">false</prop>
   </props>
  </property>
 </bean>  <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>  <bean id="bookTypeDAO"
  class="com.binghe.spring.booktype.BookTypeDAOImp">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>
 <bean id="bookDAO"
  class="com.binghe.spring.book.BookDAOImp">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
  <property name="bookTypeDAO">
   <ref bean="bookTypeDAOProxy" />
  </property>
 </bean>
   <bean id="bookTypeDAOProxy"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="target">
   <ref local="bookTypeDAO" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 <bean id="bookDAOProxy"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="target">
   <ref local="bookDAO" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="zhuxiao*">PROPAGATION_REQUIRED</prop>
    <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
  <bean name="/booktype"
  class="com.binghe.struts.action.booktype.BookTypeAction"
  singleton="false">
  <property name="bookTypeDAO">
   <ref bean="bookTypeDAOProxy" />
  </property>
 </bean>
 <bean name="/book"
  class="com.binghe.struts.action.book.BookAction"
  singleton="false">
  <property name="bookDAO">
   <ref bean="bookDAOProxy" />
  </property>
 </bean>
 </beans>
4.hibernate的設定檔為POJOClassName.bhm.xml,POJOClassName是你定義的一個javabean,你可以把這個設定檔放在和pojobean一個目錄下,也可以放在其他目錄,然後引用,格式如下:
<hibernate-mapping package="com.binghe.hibernate.reader">  <class name="Reader" table="reader" lazy="false">
  <id name="id" column="id" type="integer">
   <generator class="native" />
  </id>   <property name="code" column="code" type="string"
   not-null="false" />
  <property name="name" column="name" type="string"
   not-null="false" />
  <property name="userId" column="userid" type="integer"
   not-null="false" />
  <property name="typeId" column="typeid" type="integer"
   not-null="false" />
  <many-to-one name="user" class="com.binghe.utils.UserBean"
   insert="false" update="false">
   <column name="userid" />
  </many-to-one>
  <many-to-one name="readerType"
   class="com.binghe.hibernate.readertype.ReaderType" insert="false"
   update="false">
   <column name="typeid" />
  </many-to-one>
  <set name="borrowBills" lazy="false"
   inverse="true" cascade="none">
   <key column="readerid" />
   <one-to-many
    class="com.binghe.hibernate.borrowbill.BorrowBill" />
  </set> </hibernate-mapping>
上面的所有配置資訊是我從自己做的一個項目中抽取出來的
spring中的依賴注入/控制反轉是不錯的,你可以定義任何介面,然後實現介面裡面的方法,通過spring的設定檔把你的介面注入到任何地方,前提是你引用的必須是介面,在引用的地方必須有介面定義以及getter方法,不過你可以把這個介面當作類似javabean的一個屬性一樣來使用,javabean都有getter和setter方法的
spring 裡面的事務代理也挺挺不錯的
<property name="target">
   <ref local="bookTypeDAO" />
</property>
target它是指向要注入的類,代理這個類所實現的介面
<property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
transactionAttributes是對所代理的方法哪些方法提供事務,比如你定義一個以add開頭的方法,那它就可以有交易管理了,對於它裡面的所有操作,都可以實現事務機制,若有異常就復原事務
hibernate 的映射機制是orm,物件導向的資料庫查詢,查詢時必須提供查詢類(如find方法"from Book"hql語句中的Book不是表名而是類名),以得到它的執行個體,對應資料庫的javabean的屬性必須都是對象型的,int、double必須定義為Integer和Double類型,映射表有個lazy屬性應該設定false,不然在查詢資料庫後載入一條記錄時會報錯。一對多雙相關聯:
一對多映射,複雜
<set name="borrowBills" lazy="false"
   inverse="true" cascade="none">
   <key column="readerid" />
   <one-to-many
    class="com.binghe.hibernate.borrowbill.BorrowBill" />
  </set>
cascade屬性不好用,如果有刪除記錄操作時我都把它設定成none值,雙方都要設定,否則報錯,因為沒法相互連知
多對一映射,簡單
<many-to-one name="name" type="com.ClassName" update="false" insert="false">
<column name="nameId" />
</many-to-one>   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.