当前位置: 代码迷 >> 综合 >> Spring3与Hibernate4整合,Spring中配置Hibernate基于XML和annotation的sessionFactory方式。
  详细解决方案

Spring3与Hibernate4整合,Spring中配置Hibernate基于XML和annotation的sessionFactory方式。

热度:13   发布时间:2024-01-17 21:24:40.0


在Spring配置文件中创建sessionFactory的bean,其中Hibernate基于XML创建的sessionFactory配置代码如下:

<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!-- 依赖注入数据源,注入上文中的mysqldataSource --><property name="dataSource" ref="mysqldataSource"/><!-- mappingResources属性用来列出全部映射文件 -->   <property name="mappingResources"><list><value>com/syyyy/model/Person.hbm.xml</value></list></property><!-- 配置Hibernate属性 --><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=true;</value></property></bean>

Hibernate基于annotation注解创建的sessionFactory配置代码如下:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!-- 依赖注入数据源,注入上文中的mysqldataSource --><property name="dataSource" ref="mysqldataSource"/>
     <!-- annotatedClasses属性用来指定全部的映射类 -->
 <property name="annotatedClasses">
<list>
<value> com.syyyy.model.Person </value>
</list>
</property>
<!-- 配置Hibernate属性 --> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true; </value> </property> </bean>