spring 整合hibernate的方式

Spring中整合hibernate框架的时候,需要配置sessionFactory的相关信息+事务管理器的相关信息

有两种方式配置sessionFactory

    -----------------------------------------------------------------------------------------------------------

方法一:在spring的applicationContext.xml中配置。

推荐使用,因为使用这种方式可以使用事务管理器。

<!--读入配置文件 -->
    <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value>classpath*:jdbc.properties</value>
            </property>
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">  

         <property name="driverClassName" value="${jdbc.driverClassName}" />
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.username}" />
         <property name="password" value="${jdbc.password}" />
    </bean> 

    <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl">update</prop>
            </props>
        </property>
        <property name="mappingResources">
             <list>
                <value>classpath*:/test/domain/MyBean.hbm.xml</value>
                <value>classpath*:/test/domain/BasicBean.hbm.xml</value>
            </list>
        </property>
    </bean>

有时我们使用value,有时使用ref。

二者的区别,请参考:https://blog.csdn.net/qq_36098284/article/details/80678466

      -----------------------------------------------------------------------------------------------------------


方法二:在hibernate.cfg.xml中配置sessionFactory

首先在spring的applicationContext.xml文件中写如下的一个bean。找到我们的hibernate.cfg.xml的位置。在该文件中写hibernate的核心配置内容。

<bean id="sessionFactory"
 class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="configLocation"  value="classpath:hibernate.cfg.xml">
   </property>
</bean>


在hibernate.cfg.xml文件中配置sessionFactory。

  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!-- 指定Hibernate配置文件的DTD信息 -->  
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  5.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  6. <!-- hibernate- configuration是连接配置文件的根元素 -->  
  7. <hibernate-configuration>  
  8.     <session-factory>  
  9.         <!-- 指定连接数据库所用的驱动 -->  
  10.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  11.         <!-- 指定连接数据库的url,hibernate连接的数据库名 -->  
  12.         <property name="connection.url">jdbc:mysql://localhost/数据库名</property>  
  13.         <!-- 指定连接数据库的用户名 -->  
  14.         <property name="connection.username">root</property>  
  15.         <!-- 指定连接数据库的密码 -->  
  16.         <property name="connection.password">32147</property>  
  17.         <!-- 指定连接池里最大连接数 -->  
  18.         <property name="hibernate.c3p0.max_size">20</property>  
  19.         <!-- 指定连接池里最小连接数 -->  
  20.         <property name="hibernate.c3p0.min_size">1</property>  
  21.         <!-- 指定连接池里连接的超时时长 -->  
  22.         <property name="hibernate.c3p0.timeout">5000</property>  
  23.         <!-- 指定连接池里最大缓存多少个Statement对象 -->  
  24.         <property name="hibernate.c3p0.max_statements">100</property>  
  25.         <property name="hibernate.c3p0.idle_test_period">3000</property>  
  26.         <property name="hibernate.c3p0.acquire_increment">2</property>  
  27.         <property name="hibernate.c3p0.validate">true</property>  
  28.         <!-- 指定数据库方言 -->  
  29.         <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>  
  30.         <!-- 根据需要自动创建数据表 -->  
  31.         <property name="hbm2ddl.auto">update</property>  
  32.         <!-- 显示Hibernate持久化操作所生成的SQL -->  
  33.         <property name="show_sql">true</property>  
  34.         <!-- 将SQL脚本进行格式化后再输出 -->  
  35.         <property name="hibernate.format_sql">true</property>  
  36.         <!-- 罗列所有的映射文件 -->  
  37.         <mapping resource="映射文件路径/News.hbm.xml"/>  
  38.     </session-factory>  
  39. </hibernate-configuration>  

       ---------------------------------------------------------------------------------------------------------

配置完hibernate的核心配置文件后,还要对他的事务管理器进行配置,让spring管理我们的事务提交等问题。以至于我们不需要每次都先开启事务,处理,保存数据,提交这些操作了。

详细请参考:https://blog.csdn.net/qq_36098284/article/details/80680794

        ---------------------------------------------------------------------------------------------------------

参考:https://www.cnblogs.com/feitianshaoxai/p/6604871.html

参考:https://blog.csdn.net/wangchuanqi1234/article/details/51131285

猜你喜欢

转载自blog.csdn.net/qq_36098284/article/details/80677481
今日推荐