版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/85316544
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>