您的位置:首页 > 编程语言 > Java开发

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

2013-04-13 21:04 896 查看
在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.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.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>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: