在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"/><property name="annotatedClasses"><!-- 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>