【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明

时间:2021-10-04 21:42:18
摘要

在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍。

1. sessionFactory和数据库对应,有多少个数据库,就需要配置多少个sessionFactory;

2. session相当于数据库连接,进行数据库的CRUD操作时,需要开启session,用完需要关闭session;
3. 配置sessionFactory,主要要配置如下三个方面:
  3.1. 数据源配置;
  3.2. Hibernate属性配置;
  3.3. 映射文件配置;
  (3.4  一般还需要进行事务配置)

关于sessionFactory的类型

Hibernate映射文件可以有两种方式:1. @注释 和  2.xml配置文件;
使用两种不同方式配置时,sessionFactory的类型不同,具体如下:
@注释方式:
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 
 XML文件方式:
org.springframework.orm.hibernate3.LocalSessionFactoryBean 

见程序:(applicationContext-xxx.xml)

XML方式

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="sessionFactory"
  6. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  7. <property name="dataSource">
  8. <ref bean="dataSource" />
  9. </property>
  10. <property name="hibernateProperties">
  11. <props>
  12. <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
  13. <prop key="connection.pool_size">100</prop>
  14. <prop key="hibernate.jdbc.batch_size">100</prop>
  15. <prop key="hibernate.show_sql">false</prop>
  16. <prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
  17. <省略...>
  18. </props>
  19. </property>
  20. <property name="mappingDirectoryLocations">
  21. <list>
  22. <value>classpath:/config/hibernate-config-sql2000</value>
  23. </list>
  24. </property>
  25. </bean>
  26. </beans>


@注解方式

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="sessionFactory"
  6. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  7. <property name="dataSource">
  8. <ref bean="dataSource" />
  9. </property>
  10. <property name="hibernateProperties">
  11. <props>
  12. <!-- 此处ORACLE与SQL数据库应该区别 -->
  13. <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
  14. <!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> -->
  15. <prop key="connection.pool_size">100</prop>
  16. <prop key="hibernate.jdbc.batch_size">100</prop>
  17. <prop key="hibernate.show_sql">true</prop>
  18. <prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
  19. <省略...>
  20. </props>
  21. </property>
  22. <!--
  23. <property name="mappingResources">
  24. <list>
  25. <value>hibernate-config/DistrictArea.hbm.xml</value>
  26. </list>
  27. </property>
  28. -->
  29. <property name="mappingDirectoryLocations">
  30. <list>
  31. <value>classpath:/config/hibernate-config</value>
  32. </list>
  33. </property>
  34. </bean>
  35. <bean id="transactionManager"
  36. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  37. <property name="sessionFactory" ref="sessionFactory" />
  38. </bean>
  39. </beans>