spring整合hibernate

整合的核心思想是:

  使sessionFactory作为一个bean配置在spring的配置文件中,并且使hibernate使用上spring的声明式事务

  (只有在事务内,才能使用与当前线程绑定的session,即getCurrentSession()方法)

导入的包就是各自必须的包即可

note.txt

spring整合hibernate的方法

1.hibernate方面
    01.导入hibernate必须的jar包,和数据库,数据库连接池的jar包
    02.hibernate.cfg.xml中不用写数据库链接和关联hbm.xml的信息
        <session-factory>
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
            <property name="hbm2ddl.auto">update</property>
        </session-factory>
    03.写持久化类,和对应的hbm.xml文件
2.spring方面
    01.导入必须的jar包
    02.配置文件中这样写
    
        <!-- 
        spring整合hibernate。主要是将原来在hibernate.cfg.xml中配置的sessionFactory和关联的hbm.xml文件在
        spring的配置文件中进行配置,并且加入了事务
         -->
    
        <!-- 1.配置数据源 -->
        <context:property-placeholder location="classpath:db.properties"/>
        <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="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
        </bean>
        
        <!-- 2.配置sessionFactory的bean        LocalSessionFactoryBean  -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            
            <!-- 01.关联数据源 -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- 02.关联hibernate配置文件 -->
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
            
            <!-- 03.关联hbm.xml文件,!!主要要用/,即文件目录的方式写这一项 -->
            <property name="mappingLocations" value="classpath:sh_model/*.hbm.xml"></property>
        </bean>
        
        
        <!-- 3.配置事务 -->
            
        <!-- 01.配置事务管理器的bean -->
        <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"></property>
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <!-- 02.配置事务的属性 -->
        <tx:advice transaction-manager="transactionManager" id="txAdvice">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- 03.把属性和切面关联起来 -->
        <aop:config>
            <aop:pointcut expression="execution(* sh_service.*.*(..))" id="pointcut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
        </aop:config>
        
    <!-- 
    spring-hibernate事务的处理流程
    1.在方法开始之前
        01.获取session
        02.把session和当前线程绑定,这样才可以使用sessionFactory.getCurrentSession();来获取session
        03.开启事务
    2.若方法正常结束
        01.提交事务
        02.解除session绑定
        03.关闭session
    3.若方法出现异常
        01.回滚事务
        02.解除session绑定
        03.关闭session
        
    因此,若是DAO不在事务内,那么sessionFactory.getCurrentSession();就无法获取session
     -->
!补充:
    如何完全不使用hibernate.cfg.xml
    1.去掉<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>这一句
    2.这时hibernate.的前缀是不能省的
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">false</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>        

spring配置文件applicationContext_SH.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 
spring整合hibernate。主要是将原来在hibernate.cfg.xml中配置的sessionFactory和关联的hbm.xml文件在
spring的配置文件中进行配置,并且加入了事务
 -->
    
    <context:component-scan base-package="db_DAO.impl"></context:component-scan>
    
    <!-- 1.配置数据源 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <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="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
    </bean>
    
    <!-- 2.配置sessionFactory的bean  -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        
        <!-- 01.关联数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        
        <!-- 02.关联hibernate配置文件 -->
        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
            <!-- 如何完全不使用hibernate.cfg.xml,也就无需    02.关联hibernate配置文件        这一项 -->
            <!-- 这时hibernate.的前缀不能省 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
        
        <!-- 03.关联hbm.xml文件,!!主要要用/,即文件目录的方式写这一项 -->
        <property name="mappingLocations" value="classpath:sh_model/*.hbm.xml"></property>
    </bean>
    
    
    <!-- 3.配置事务 -->
        
        <!-- 01.配置事务管理器的bean -->
        <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"></property>
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <!-- 02.配置事务的属性 -->
        <tx:advice transaction-manager="transactionManager" id="txAdvice">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- 03.把属性和切面关联起来 -->
        <aop:config>
            <aop:pointcut expression="execution(* db_DAO.impl.*.*(..))" id="pointcut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
        </aop:config>
    <!-- 
        spring-hibernate事务的处理流程
        1.在方法开始之前
            01.获取session
            02.把session和当前线程绑定,这样才可以使用sessionFactory.getCurrentSession();来获取session
            03.开启事务
        2.若方法正常结束
            01.提交事务
            02.解除session绑定
            03.关闭session
        3.若方法出现异常
            01.回滚事务
            02.解除session绑定
            03.关闭session
            
        因此,若是DAO不在事务内,那么sessionFactory.getCurrentSession();就无法获取session
     -->
</beans>

hibernate的配置文件hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
    
    </session-factory>
</hibernate-configuration>

 

posted @ 2017-03-13 18:27  逢甘霖成大事  阅读(224)  评论(0编辑  收藏  举报