<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Spring學習筆記系列(五) 與hibernate整合 a

    在學習這一部分的時候我作了一個用StrutsAction訪問UserDAO中方法,此方法使用了hibernateTemplate。調(diào)試過程中問題多多,好在一個一個解決了。
    JPetStore2.0 已經(jīng)有ibatis做為OR層了,我要換成hibernate需要修改Spring配置文件中的bean id="TransactionManager" 、增加bean id=“sessionFactory”。又因為配置文件id=TransactionManager的bean只能有一個,修改為hibernate后 原來使用ibatis的bean就都不好用了,所以我新創(chuàng)建了一個空的配置文件dataAccessContext-hibernate.xml。只有幾 個字定義的bean,如下:

     <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
    http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

     <!-- ========================= RESOURCE DEFINITIONS ========================= -->

     <!-- Local Apache Commons DBCP DataSource that refers to a combined database -->
     <!-- (see dataAccessContext-jta.xml for an alternative) -->
     <!-- The placeholders are resolved from jdbc.properties through -->
     <!-- the PropertyPlaceholderConfigurer in applicationContext.xml -->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
      <property name="url"><value>jdbc:mysql://localhost:3306/jpetstore</value></property>
      <property name="username" ><value>root</value></property>
      <property name="password" ><value>123456</value></property>
     </bean>
     
     
     <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="dataSource">
       <ref local="dataSource"/>
      </property>
      <property name="mappingResources">
       <list>
        <value>srx/test/hibernate/Account.hbm</value>
       </list>
      </property>
      <property name="hibernateProperties">
       <props>
        <prop key="hibernate.dialect">
         net.sf.hibernate.dialect.MySQLDialect
        </prop>
        <prop key="hibernate.showsql">
         true
        </prop>
       </props>
      </property>
     </bean>
     
        <!-- Transaction manager for a single JDBC DataSource -->
     <!-- (see dataAccessContext-jta.xml for an alternative) -->
     <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
      <property name="sessionFactory">
       <ref local="sessionFactory"/>
      </property>
     </bean>
     
     
     
     <!-- this bellow is hibernate configuration for srx test-->
     
     
     <bean id="userDAO" class="srx.test.testhibernate.UserDAO">
      <property name="sessionFactory">
       <ref local="sessionFactory"/>
      </property>
     </bean>
    </beans>

    原來包含很多復雜內(nèi)容的applicationContext.xml也拷貝一份applicationContext2.xml,刪除和JPetStore相關的內(nèi)容,留下通用的部分:

     <?xml version="1.0" encoding="UTF-8"?>

    <!--
      - Application context definition for JPetStore's business layer.
      - Contains bean references to the transaction manager and to the DAOs in
      - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
      -->
    <beans xmlns="
    http://www.springframework.org/schema/beans"
        xmlns:xsi="
    http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="
    http://www.springframework.org/schema/aop"
        xmlns:tx="
    http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
       <list>
        <value>WEB-INF/mail.properties</value>
        <value>WEB-INF/jdbc.properties</value>
       </list>
      </property>
     </bean>
     
     <aop:config>
      <aop:advisor pointcut="execution(* *..PetStoreImpl.*(..))" advice-ref="txAdvice"/>
     </aop:config>

     <!--
      Transaction advice definition, based on method name patterns.
      Defaults to PROPAGATION_REQUIRED for all methods whose name starts with
      "insert" or "update", and to PROPAGATION_REQUIRED with read-only hint
      for all other methods.
     -->
     <tx:advice id="txAdvice">
      <tx:attributes>
       <tx:method name="insert*"/>
       <tx:method name="update*"/>
       <tx:method name="*" read-only="true"/>
      </tx:attributes>
     </tx:advice>
    </beans>

    在Struts配置文件中增加自己的Action如下:
     <action path="/showusers" type="srx.test.struts.action.UserAction">
        <forward name="success" path="/WEB-INF/jsp/srx/test/hibernate/showusers.jsp"/>
      </action>
      

    web.xml中使用action作為*。do處理的servlet而不是默認的petstore。
    并注釋掉名字為petstore,remoting的servlet。如下:

     <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

     <display-name>Spring JPetStore</display-name>

     <description>Spring JPetStore sample application</description>
     
     <context-param>
      <param-name>webAppRootKey</param-name>
      <param-value>petstore.root</param-value>
     </context-param>

     <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>/WEB-INF/log4j.properties</param-value>
     </context-param>

     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
       /WEB-INF/dataAccessContext-hibernate.xml
      </param-value>
     </context-param>

     <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     

     <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <load-on-startup>3</load-on-startup>
     </servlet>

     <servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>*.do</url-pattern>
     </servlet-mapping>
     ...

    </web-app>

    posted on 2007-06-18 18:01 chenguo 閱讀(170) 評論(0)  編輯  收藏 所屬分類: Spring Dev

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統(tǒng)計

    留言簿

    隨筆分類(1)

    文章分類(52)

    好友 小山的博客

    最新隨筆

    最新評論

    主站蜘蛛池模板: 狠狠综合久久综合88亚洲| 四虎永久免费影院在线| 久久笫一福利免费导航| 美女视频黄的全免费视频| 毛片免费在线观看网址| 免费人成在线观看播放国产| 国产亚洲欧洲Aⅴ综合一区| 亚洲成色在线影院| 亚洲色欲啪啪久久WWW综合网| 国产午夜亚洲精品不卡| 两性色午夜视频免费网| 6080午夜一级毛片免费看| 暖暖免费高清日本一区二区三区| 亚洲七七久久精品中文国产| 亚洲尹人香蕉网在线视颅| 亚洲综合av一区二区三区| 成人免费网站久久久| 37pao成人国产永久免费视频| 日本免费电影一区| 亚洲国产精彩中文乱码AV| www.亚洲日本| jyzzjyzz国产免费观看| 1000部拍拍拍18勿入免费视频软件| 性xxxx视频播放免费| 亚洲精品成人无限看| 亚洲五月综合网色九月色| 一个人看的免费高清视频日本| 99精品一区二区免费视频| 国产精品久久香蕉免费播放| 亚洲AV永久无码精品水牛影视| 久久久久久亚洲精品影院| 中国性猛交xxxxx免费看| 性做久久久久久免费观看| 国产精品亚洲不卡一区二区三区| 亚洲国产美女在线观看| 一级日本高清视频免费观看| 91香蕉成人免费网站| 狠狠色婷婷狠狠狠亚洲综合| 亚洲国产乱码最新视频| a毛看片免费观看视频| 免费观看大片毛片|