Spring4整合hibernate4

1. 整合内容

  ①让Spring的IOC容器为Hibernate生成SessionFactory

  ②让Hibernate使用Spring的声明时事务

2. 整合步骤  2.1 部署Hibernate

    2.1.1 加包(Spring4和Hibernate4的包)

    

    2.1.2 添加Hibernate.cfg.xml配置文件,配置hibernate的基本属性

      (1)数据源许配置到spring的IOC容器中,不需要在hibernate中配置数据源

      (2)关联的.hbm.xml文件也在spring的IOC容器中配置

      (3)在hibernate中只需配置基本属性:数据库方言,SQL显示,生成数据表策略,二级缓存等     

<?xml version='1.0' encoding='UTF-8'?>
<!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 的基本属性 -->
        <!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 -->
        <!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例时在进行配置 -->
        <!-- 3. 配置 hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!-- 配置 hibernate 二级缓存相关的属性... -->
    </session-factory>
</hibernate-configuration>

    2.1.3 创建持久化类以及对应的.hbm.xml文件(略)

  2.2 部署Spring,以及整合hibernate

    2.2.1 加包(上面已加)

    2.2.2 加入Spring的配置文件applicationContext.xml

      (1)配置数据源

      (2)配置hibernate的SessionFactory实例(通过Spring提供的LocalSessionFactory进行配置)

      (3)配置Spring的声明时事务

<?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-4.0.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.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.atguigu.spring.hibernate"></context:component-scan>

    <!-- 导入属性文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="driverClass" value="${driverclass}"></property>

        <property name="initialPoolSize" value="${initPoolSize}"></property>
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
    </bean>

    <!-- 配置hibernate的SessionFactory实例     通过Spring提供的LocalSessionFactoryBean进行配置 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        
        <!-- 配置数据源属性 -->
        <property name="dataSource" ref="dataSource"></property>
        
        <!-- 配置hibernate的配置文件位置及名称-->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>                 
       
     <!-- 配置hibernate的映射文件的位置及名称,可以使用通配符 --> <property name="mappingLocations" value="classpath:com/atguigu/spring/hibernate/entities/*.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="get*" read-only="true" /> <!-- 事务的传播行为 --> <!-- <tx:method name="purchase" propagation="REQUIRES_NEW"/> --> <tx:method name="*" /> </tx:attributes> </tx:advice> <!-- 3.配置事务切点并且把事务切点和属性关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.spring.hibernate.service.*.*(..))" id="txPointCut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" /> </aop:config> </beans>
原文地址:https://www.cnblogs.com/cowboys/p/5494955.html