spring与hibernate整合之sessionfactory的三种方式

方式1:只加载SessionFactory
 

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

      <propertyname="configLocation" value="classpath:hibernate.cfg.xml"></property>

</bean>


方式2:加载数据库连接池

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <propertyname="dataSource" ref="dataSource"></property>

    <propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
</bean>


方式3:加载所有属性(连接池以及配置映射资源全交给spring创建)

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <!-- a. 注入连接池 -->
       <property name="dataSource" ref="dataSource"></property>
       <!-- b. hibernate常用配置:方言、自动建表、显示sql -->
       <property name="hibernateProperties">
           <props>
               <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
               <prop key="hibernate.show_sql">true</prop>
               <prop key="hibernate.hbm2ddl.auto">update</prop>
           </props>
       </property>   

       <!-- c. 加载所有的映射(根据路径加载)
       <propertyname="mappingLocations">
           <list>
               <value>classpath:cn/itcast/entity/*.hbm.xml</value>
           </list>
       </property>
       -->
       <!-- c. 根据目录加载所有的映射 -->
       <property name="mappingDirectoryLocations">
           <list>
               <value>classpath:cn/itcast/entity</value>
           </list>
       </property>
    </bean>


========================================hibernate配置数据库连接池======================

   

<session-factory>
        <!-- 一、 数据库连接的参数配置 -->
        <property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    
        <!-- 二、 hibernate其他常用配置 -->
        <property name="hibernate.show_sql">true</property>
        <!--<property name="hibernate.format_sql">true</property>-->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!--
            hibernate对连接池的支持
         -->
         <!-- C3p0连接池支持类 -->
         <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
         <!-- 最大连接数 -->
         <property name="hibernate.c3p0.max_size">6</property>
         <!-- 最小连接数 -->
         <property name="hibernate.c3p0.min_size">4</property>
         <!-- 当连接不够用时候每次的增量 -->
         <property name="hibernate.c3p0.acquire_increment">2</property>
         <!-- 最多执行的命令的个数 -->
         <property name="hibernate.c3p0.max_statements">100</property>
         <!-- 连接空闲测试时间 -->
         <property name="hibernate.c3p0.idle_test_period">3000</property>
  
         <!-- 配置session的创建方式,线程方式创建session -->
         <property name="hibernate.current_session_context_class">thread</property>
   </session-factory>

猜你喜欢

转载自blog.csdn.net/weixin_40234548/article/details/85083610
今日推荐