Spring4 整合 Hibernate4

1. Spring 整合 Hibernate 整合什么 ?

1). 由 IOC 容器来管理 Hibernate 的 SessionFactory

2). 让 Hibernate 使用上 Spring 的声明式事务

2. 整合步骤:

1). 加入 hibernate

(1). jar 包(所有required 路径下的包)

Spring4 整合 Hibernate4_第1张图片

 

再导入 MySQL 数据库驱动的包 和 c3p0 数据连接池的包

(2). 添加 hibernate 的配置文件:hibernate.cfg.xml(也可以不维护该配置文件,而是在 spring 的配置文件中的 SessionFactory 实例中配置)

 
   

<session-factory>

   

   

   

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialectproperty>

    <property name="hibernate.show_sql">trueproperty>

    <property name="hibernate.format_sql">trueproperty>

    <property name="hibernate.hbm2ddl.auto">updateproperty>

   

   

   

    session-factory>


(3). 编写了持久化类对应的 .hbm.xml 文件。


2). 加入 Spring

(1). jar 包

Spring4 整合 Hibernate4_第2张图片


(2). 加入 Spring 的配置文件

	 

         <context:component-scan base-package="spring.hibernate">context:component-scan>

        

         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

             <property name="driverClass" value="${jdbc.driverClassName}"/>

             <property name="jdbcUrl" value="${jdbc.url}"/>

<property name="user" value="${jdbc.username}"/>

             <property name="password" value="${jdbc.password}"/>

             <property name="minPoolSize" value="${jdbc.minPoolSize}"/>

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

         bean>

         <context:property-placeholder location="jdbc.properties"/>

        

         <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

                  

                   <property name="dataSource" ref="dataSource">property>

                  

                  

                  

                   <property name="hibernateProperties">

<props>                                   

                               <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialectprop>

                               <prop key="hibernate.show_sql">trueprop>

                               <prop key="hibernate.format_sql">trueprop>

                               <prop key="hibernate.hbm2ddl.auto">updateprop>

                        props>

                   property>


        < property  name = "mappingLocations"  value = "classpath:spring/hibernate/entity/*.hbm.xml" > property >

          bean >

         

         

         <bean id="transactionManager"

                 class="org.springframework.orm.hibernate4.HibernateTransactionManager">

                   <property name="sessionFactory" ref="sessionFactory">property>

         bean>

         

         <tx:advice id="txAdvice">

                   <tx:attributes>

                            <tx:method name="get*" read-only="true"/>

                            <tx:method name="purchase" propagation="REQUIRES_NEW"/>

                            <tx:method name="*"/>

                   tx:attributes>

         tx:advice>


         

         <aop:config>

                   <aop:pointcut expression="execution(* spring.hibernate.service.*.*(..))"

                            id="txPointcut"/>

                   <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>

         aop:config>




你可能感兴趣的:(Java)