首页 > 开发 > JSP > 正文

JSP教程之整合hibernate持久层1

2020-05-06 19:54:31
字体:
来源:转载
供稿:网友
整合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>

 

共2页上一页12下一页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表