首页 > 代码库 > java深入探究12-框架整合

java深入探究12-框架整合

1.Spring与Hibernate整合

  需要配置的就是hibernate和bean.xml

  1)关键点:sessionFactory创建交给SpringIOC;session的事务处理交给Spring的事务处理

  2)jar包:  

    连接池/数据库驱动包

    Hibernate相关jar

    Spring 核心包(5)

    Spring aop (4)

    spring-orm-3.2.5.RELEASE.jarspringhibernate的支持】

spring-tx-3.2.5.RELEASE.jar【事务相关】

  3)整合步骤:A:创建sessionFactory 的bean节点;B:对逻辑层的业务方法添加事务

    A:sessionFactory整合在Spring bean中的三种方式:

    1.全部hibernate.cfg.xml引入bean.xml中

    2.部分引入:dataSource连接池写在Spring中

    3.全部在bean.xml中写

    B:对逻辑层添加事务用aop事务方式

  4)例子:  

技术分享
1.bean.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- dao 实例 -->
    <bean id="deptDao" class="dao.DeptDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- service 实例 -->
    <bean id="deptService" class="cn.itcast.service.DeptService">
        <property name="deptDao" ref="deptDao"></property>
    </bean>
    <!-- 数据源配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value=http://www.mamicode.com/"com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value=http://www.mamicode.com/"jdbc:mysql:///hib_demo"></property>
        <property name="user" value=http://www.mamicode.com/"root"></property>
        <property name="password" value=http://www.mamicode.com/"root"></property>
        <property name="initialPoolSize" value=http://www.mamicode.com/"3"></property>
        <property name="maxPoolSize" value=http://www.mamicode.com/"10"></property>
        <property name="maxStatements" value=http://www.mamicode.com/"100"></property>
        <property name="acquireIncrement" value=http://www.mamicode.com/"2"></property>
    </bean>
    
    <!-- ###########Spring与Hibernate整合  start########### -->
    <!-- 方式一:直接加载hibernate.cfg.xml文件的方式整合   这种方式 -->
    <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value=http://www.mamicode.com/"classpath:hibernate.cfg.xml"></property>
    </bean> -->
    
    <!-- 方式二:连接池交给Spring来生成【一部分配置写到hibernate中,一份分在spring中完成】 -->
    <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value=http://www.mamicode.com/"classpath:hibernate.cfg.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean> -->
    
    <!-- 推荐 方式三:所有配置全部写到Spring配置中 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 连接池配置 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- hibernate常用配置会 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- hibernate映射配置 -->
        <!-- hibernate映射配置 
        <property name="mappingLocations">
            <list>
                <value>classpath:entity/*.hbm.xml</value>
            </list>
        </property>
        -->
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:entity/</value>
            </list>
        </property>
        
        
    </bean>
    
    <!-- ###########Spring与Hibernate整合  end########### -->
    <!-- 事务配置 -->
    <!-- a. 配置事务管理器类 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- b. 配置事务增强(拦截到方法后如果管理事务?) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <!-- c. Aop配置 -->
    <aop:config>
         <aop:pointcut expression="execution(* service.*.*(..))" id="pt"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
</beans>     
View Code

2.SSH

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"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.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- dao 实例 --><bean id="deptDao" class="dao.DeptDao"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- service 实例 --><bean id="deptService" class="cn.itcast.service.DeptService"><property name="deptDao" ref="deptDao"></property></bean><!-- 数据源配置 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="http://www.mamicode.com/com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="http://www.mamicode.com/jdbc:mysql:///hib_demo"></property><property name="user" value="http://www.mamicode.com/root"></property><property name="password" value="http://www.mamicode.com/root"></property><property name="initialPoolSize" value="http://www.mamicode.com/3"></property><property name="maxPoolSize" value="http://www.mamicode.com/10"></property><property name="maxStatements" value="http://www.mamicode.com/100"></property><property name="acquireIncrement" value="http://www.mamicode.com/2"></property></bean><!-- ###########Spring与Hibernate整合  start########### --><!-- 方式一:直接加载hibernate.cfg.xml文件的方式整合   这种方式 --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation" value="http://www.mamicode.com/classpath:hibernate.cfg.xml"></property></bean> --><!-- 方式二:连接池交给Spring来生成【一部分配置写到hibernate中,一份分在spring中完成】 --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation" value="http://www.mamicode.com/classpath:hibernate.cfg.xml"></property><property name="dataSource" ref="dataSource"></property></bean> --><!-- 推荐 方式三:所有配置全部写到Spring配置中 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 连接池配置 --><property name="dataSource" ref="dataSource"></property><!-- hibernate常用配置会 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><!-- hibernate映射配置 --><!-- hibernate映射配置 <property name="mappingLocations"><list><value>classpath:entity/*.hbm.xml</value></list></property>--><property name="mappingDirectoryLocations"><list><value>classpath:entity/</value></list></property></bean><!-- ###########Spring与Hibernate整合  end########### --><!-- 事务配置 --><!-- a. 配置事务管理器类 --><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- b. 配置事务增强(拦截到方法后如果管理事务?) --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="*" read-only="false"/></tx:attributes></tx:advice><!-- c. Aop配置 --><aop:config> <aop:pointcut expression="execution(* service.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/></aop:config></beans>     









java深入探究12-框架整合