Spring学习笔记 五 整合hibernate持久层 ----1,基本配置

在学习这一部分的时候我作了一个用StrutsAction访问UserDAO中方法,此方法使用了hibernateTemplate。调试过程中问题多多,好在一个一个解决了。
JPetStore2.0已经有ibatis做为OR层了,我要换成hibernate需要修改Spring配置文件中的bean id="TransactionManager" 、增加bean id=“sessionFactory”。又因为配置文件id=TransactionManager的bean只能有一个,修改为hibernate后原来使用ibatis的bean就都不好用了,所以我新创建了一个空的配置文件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>

原来包含很多复杂内容的applicationContext.xml也拷贝一份applicationContext2.xml,删除和JPetStore相关的内容,留下通用的部分:

 <?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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值