Spring4整合Hibernate4

首先,要明确Spring整合Hibernate可以做什么?

答案是:

  1.由IOC容器来管理Hibernate的SessionFactory
  2.让Hibernate使用上Spring的声明式事务

整合的过程以一个实例来说明。

在整合的中,hibernate的一些配置都可以放在spring的配置文件中。但是为了使配置文件看起啦比较清晰,建议还是分开存放。

比如在db.properties中存放数据库信息,hibernate.cfg.xml中存放hibernate的基本信息。其余的配置信息可以放在spring的配置文件(applicationContext.xml)中。

db.properties

1 jdbc.user=yulei
2 jdbc.password=yulei
3 jdbc.driverClass=oracle.jdbc.driver.OracleDriver
4 jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
5 
6 jdbc.initialPoolSize=5
7 jdbc.maxPoolSize=10

hibernate.cfg.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <!-- 配置连接数据库的基本信息 -->
 8         <!-- 
 9             1.数据源配置到IOC容器中,所以此处不再需要配置数据源
10             2.关联的.hbm.xml也在IOC容器配置SessionFactory实例时再进行配置
11             3.配置Hibernate的基本属性:方言、SQL显式及格式化、生成数据表的策略以及二级缓存等
12          -->
13         
14     
15         <!-- 配置hibernate基本信息 -->
16         <!-- hibernate所使用的数据库方言 -->
17         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
18         
19         <!-- 执行操作时是否在控制台打印sql -->
20         <property name="show_sql">true</property>
21         
22         <!-- 是否对SQL进行格式化 -->        
23         <property name="format_sql">true</property>
24         
25         <!-- 指定自动生成数据表的策略 -->
26         <property name="hbm2ddl.auto">update</property>
27         
28         
29     </session-factory>
30 </hibernate-configuration>

applicationContext.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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.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.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.yl.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="${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 配置文件的名称及位置 -->
        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
        <!-- 使用hibernateProperties属性来配置Hibernate原生的属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        
        <!-- 配置 hibernate 映射文件的位置及名称 , 可以使用通配符-->
        <property name="mappingLocations" value="classpath:com/yl/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.yl.spring.hibernate.service.*.*(..))" 
            id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
    
    

</beans>

以上三个配置文件基本上就是Spring整合Hibernate所需的配置。

posted @ 2014-12-04 21:58  _freedom_yl  阅读(1336)  评论(0编辑  收藏  举报