Spring4深入理解----整合Hibernate

参考代码下载github:https://github.com/changwensir/java-ee/tree/master/spring4

Spring 支持大多数流行的 ORM 框架 , 包括 HibernateJDO, TopLink , Ibatis JPA
Spring 对这些 ORM 框架的支持是一致的 , 因此可以把和 Hibernate 整合技术应用到其他 ORM 框架上 .
Spring 2.0 同时支持 Hibernate2.x 3.x. Spring2.5 只支持 Hibernate3.1 或更高版本
Spring中配置SessionFactory

    • 对于 Hibernate 而言 , 必须从原生的 HibernateAPI 中构建 SessionFactory . 此外 , 应用程序也无法利用 Spring 提供的数据存储机制 ( 例如 :Spring 的事务管理机制 )
    • Spring 提供了对应的工厂 Bean, 可以用单实例的形式在 IOC 容器中创建 SessionFactory 实例 .

hibernate.cfg.xml,实际开发的时候最好保留这个文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 配置 utils 的基本属性 -->
        <!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 -->
        <!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例时在进行配置 -->
        <!-- 3. 配置 utils 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

    </session-factory>
</hibernate-configuration>
application.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

       <!-- 配置自动扫描的包 -->
       <context:component-scan base-package="Spring4_hibernate"/>

       <!--导入资源文件-->
       <context:property-placeholder location="classpath:Spring4_hibernate/db.properties"/>
       <!--配置C3P0数据源-->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <property name="user" value="${jdbc.user}"/>
              <property name="password" value="${jdbc.password}"/>
              <property name="driverClass" value="${jdbc.driverClass}"/>
              <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>

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

       <!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">              <!-- 1、配置数据源属性 -->
              <property name="dataSource" ref="dataSource"/>
              <!-- 2、配置 utils 配置文件的位置及名称 -->

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

              <!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 -->
              <!--<property name="hibernateProperties">-->
                     <!--<props>-->
                            <!--<prop key="utils.dialect">org.utils.dialect.MySQL5InnoDBDialect</prop>-->
                            <!--<prop key="utils.show_sql">true</prop>-->
                            <!--<prop key="utils.format_sql">true</prop>-->
                            <!--<prop key="utils.hbm2ddl.auto">update</prop>-->
                     <!--</props>-->
              <!--</property>-->
              <!-- 3、配置 utils 映射文件的位置及名称, 可以使用通配符 -->
              <property name="mappingLocations"
                        value="classpath:Spring4_hibernate/*.hbm.xml"/>

       </bean>

       <!-- 配置 Spring 的声明式事务 -->
       <!-- 1. 配置事务管理器 -->
       <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"/>
       </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(* Spring4_hibernate.service.*.*(..))"
                            id="txPointcut"/>
              <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
       </aop:config>
</beans>





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值