1. 程式人生 > >Spring整合hibernate的時候核心配置檔案可以不用獨立存在

Spring整合hibernate的時候核心配置檔案可以不用獨立存在

Spring整合hibernate的時候核心配置檔案可以不用獨立存在

一般整合的時候,既有applicationContext.xml也有hiberate.cfg.xml

hiberate.cfg.xml檔案中有

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

    <session-factory>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  <!--   <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> -->
     <property name="hibernate.hbm2ddl.auto">update</property>
    
    </session-factory>

</hibernate-configuration>

,但是這樣寫的話,感覺配置檔案太多了;可以不用這個hiberate.cfg.xml檔案;

在applicationContext.xml配置如下

<!-- 配置hibernate的sessionFactory例項 -->
	<bean id="localSessionFactoryBean"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="da"></property>
		<!-- 可以不需要用到hibernate.cfg.xml檔案,只需要用hibernateproperties -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto"></prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
			</props>
		</property>
<!-- 		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
		<property name="mappingLocations" value="classpath*:cn/com/spring/hibernate/*.hbm.xml">
		</property>
	</bean>