• Spring与Hibernate整合,实现Hibernate事务管理


    1.所需的jar包

    连接池/数据库驱动包

    Hibernate相关jar

    Spring 核心包(5个)

    Spring aop 包(4个)

    spring-orm-3.2.5.RELEASE.jar                 【spring对hibernate的支持】

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

    2.配置文件

    Product.hbm.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.juaner.entity">
        <class name="Product">
            <id name="id" column="id">
                <generator class="native"/>
            </id>
            <property name="name" column="name"/>
            <property name="price" column="price"/>
            <property name="remark" column="remark"/>
        </class>
    
    </hibernate-mapping>

    hibernate.cfg.xml

    <!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>
            <!-- 数据库连接信息 -->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>    
            <property name="hibernate.connection.url">jdbc:mysql:///test</property>
            <property name="hibernate.connection.username">root</property>    
            <property name="hibernate.connection.password">juaner767</property>
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.hbm2ddl.auto">update</property>
            <!--spring默认以线程方式创建,配了反而有问题-->
            <!-- session创建方式 -->
            <!--<property name="hibernate.current_session_context_class">thread</property>-->
            
            <!-- 加载映射 -->
            <mapping resource="com/juaner/entity/Product.hbm.xml"/>
    
        </session-factory>
    </hibernate-configuration>

    3.直接加载hibernate文件的方式配置

    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: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="productDao" class="com.juaner.dao.ProductDao">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <!--service-->
        <bean id="productService" class="com.juaner.service.ProductService">
            <property name="productDao" ref="productDao"/>
        </bean>
    
        <!--spring与hibernate整合-->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <!--通过配置文件创建sessionfactory对象-->
            <property name="configLocation" value="hibernate.cfg.xml"></property>
        </bean>
        <!--事务配置-->
        <!--配置事务管理器类-->
        <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <!--配置事务增强-->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        <!--aop配置-->
        <aop:config>
            <aop:pointcut id="pt" expression="execution(* com.juaner.service.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
        </aop:config>
    </beans>

    4.spring配置连接池和hibernate常用配置

    <?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: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="productDao" class="com.juaner.dao.ProductDao">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <!--service-->
        <bean id="productService" class="com.juaner.service.ProductService">
            <property name="productDao" ref="productDao"/>
        </bean>
    
        <!--数据源配置-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///test"/>
            <property name="user" value="root"/>
            <property name="password" value="juaner767"/>
            <property name="initialPoolSize" value="3"/>
            <property name="maxPoolSize" value="6"/>
            <property name="maxStatements" value="100"/>
            <property name="acquireIncrement" value="2"/>
        </bean>
        <!--spring与hibernate整合-->
        <!--方式2,使用spring创建连接池对象-->
        <!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">-->
            <!--&lt;!&ndash;通过配置文件创建sessionfactory对象&ndash;&gt;-->
            <!--<property name="configLocation" value="hibernate.cfg.xml"></property>-->
            <!--<property name="dataSource" ref="dataSource"/>-->
        <!--</bean>-->
        <!--方式3,-->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--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>
            <!--映射文件配置-->
            <property name="mappingDirectoryLocations">
                <list>
                    <value>classpath:com/juaner/entity/</value>
                </list>
            </property>
        </bean>
        <!--事务配置-->
        <!--配置事务管理器类-->
        <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <!--配置事务增强-->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        <!--aop配置-->
        <aop:config>
            <aop:pointcut id="pt" expression="execution(* com.juaner.service.ProductService.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
        </aop:config>
    </beans>
  • 相关阅读:
    OC中的字典
    OC中的那些String
    虚拟机资源共享
    虚拟机空间使用心得
    PEST和SWOT分析法
    Axure 的四种预览模式
    竞品分析:抖音VS快手
    第二章:行业与市场分析六步法
    第一章:互联网产品从0到1全流程解密(9-11)
    第一章:互联网产品从0到1全流程解密(5-8)
  • 原文地址:https://www.cnblogs.com/juaner767/p/5597009.html
Copyright © 2020-2023  润新知