spring整合hibernate的詳細步驟


  Spring整合hibernate需要整合些什么?

    1. 由IOC容器來生成hibernate的sessionFactory.
    2. 讓hibernate使用spring的聲明式事務

整合步驟:

  • 加入hibernate:

    1.導jar包

    2.配置hibernate的配置文件,hibernate.cfg.xml.(其實可以省略這個配置文件,但還是加上比較舒服):

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
    <!-- 配置hibernate的基本屬性 -->
        <!-- 配置數據源 ,因為需要把數據源配置到IOC容器中,所以這里不需要再配置-->
        <!-- 配置數據庫方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- 顯示並格式化sql語句 -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!-- 自動生成數據庫表結構 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 使用getCurrentSession方式打開會話 -->
        <property name="hibernate.current_session_context_class">thread</property>
        
        <!-- 關聯的*.hbm.xml也在容器配置sessionFactory實例時在進行配置,這里也不再配置 -->
    </session-factory>
</hibernate-configuration>

    3.編寫持久化類以及對應的hbm.xml.

  • 加入Spring

    1.導入jar包.

    2.配置spring配置文件(重點,詳見注釋):

      beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 導入資源文件 -->
    <context:property-placeholder location="db.properties" ignore-resource-not-found="false" />
    <!-- 配置c3p0連接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    
    <!--配置hibernate的SessionFactory實例,通過Spring提供的LocalSessionFactorybean進行配置 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <!-- 配置數據源屬性 -->
        <property name="dataSource" ref="dataSource"></property>
            <!-- 配置hibernate.cfg.xml的位置及名稱-->
        <property name="configLocation" value="hibernate.cfg.xml"></property>
            <!-- 配置hibernate映射文件的位置及名稱,可以使用通配符-->
        <property name="mappingLocations" value="com/wang/entity/*.hbm.xml"></property>
    </bean>
    <!-- 配置Spring的聲明式事務 -->
        <!-- 1.配置事務管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!-- 2.配置事務屬性,需要事務管理器 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*"/>
                <tx:method name="get*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
        <!-- 3.配置事務切點,並把切點和事務屬性關聯起來 -->
        <aop:config>
            <aop:pointcut expression="execution(* com.wang.service.*.*(..))" id="pointcut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
        </aop:config>
</beans>

到此,配置工作就完成了,接下來可以測試一下代碼,看看自動生成的數據庫表.

   如果你選擇不使用hibernate.cfg.xml,那么可以在上面的beans.xml中,刪掉<property name="configLocation" value="hibernate.cfg.xml"></property>,同時添加以下代碼: 

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM