当spring和Hibernate集成时,在applicationContext.xml和Hibernate.cfg.xml配置sessionfactory区别

所有的配置信息都在applicationContext.xml里面配置:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


 <!-- <bean id="sessionFactory" -->
 <!-- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> -->
 <!-- <property name="configLocation" value="file:src/hibernate.cfg.xml"> -->
 <!-- </property> -->
 <!-- </bean> -->


 <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/test</value>
  </property>

  <property name="username">
   <value>root</value>
  </property>

  <property name="password">
   <value>root</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/test/bean/Person.hbm.xml</value>
   </list>
  </property>

  <!-- <property name="mappingDirectoryLocations"> -->
  <!-- <list> -->
  <!-- <value>classpath:/com/test/bean/</value> -->
  <!-- </list> -->
  <!-- </property> -->


  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hbm2ddl.auto">update</prop>
   </props>

  </property>
 </bean>

 <bean id="personDao" class="com.test.dao.service.impl.PersonDaoImpl">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
</beans>

 这样可以直接链接到数据库。

 

但是当我用applicationContext.xml和hibernate.cfg.xml搭配使用时,只见提交语句,就是没有在数据库中找到保存的数据,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


 <bean id="sessionFactory"
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <property name="configLocation" value="file:src/hibernate.cfg.xml">
 </property>
 </bean>

 <bean id="personDao" class="com.test.dao.service.impl.PersonDaoImpl">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
</beans>

 

hibernate.cfg.xml配置:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

 <session-factory>
  <property name="connection.driver_class">
   com.mysql.jdbc.Driver
  </property>
  <property name="connection.url">
   jdbc:mysql://localhost:3306/test
  </property>
  <property name="connection.username">root</property>
  <property name="connection.password">root</property>

  <property name="dialect">
   org.hibernate.dialect.MySQLDialect
  </property>
  <property name="show_sql">true</property>
  <property name="hbm2ddl.auto">update</property>

  <mapping resource="com/test/bean/Person.hbm.xml" />

 </session-factory>

</hibernate-configuration>

 

最后发现如果你只单独使用applicationContext.xml时,spring会自动帮你提交事物,但是如果你同时使用的时候需要手动的在Hibernate.cfg.xml里面添加下面这段配置:

<property name="hibernate.connection.autocommit">true</property>

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值