所有的配置信息都在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>