• MyBatis-Spring 事务配置


    官方链接:http://mybatis.org/spring/zh/transactions.html#configuration

    1、依赖

      tx和aop相关配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            https://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
            这里使用的Spring提供的JDBC:org.springframeword.jdbc.datasource
            -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
    
        <!--sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!--绑定MyBatis配置文件-->
            <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
            <!--配置mapper-->
            <property name="mapperLocations" value="classpath:com/doubleh/mapper/*.xml"></property>
        </bean>
    
        <!--SqlSessionTemplate:就是我们用的sqlSession-->
        <!--<tx:jta-transaction-manager />-->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
        </bean>
    
        <!--注入DataSourceTransactionManager -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <constructor-arg ref="dataSource" />
        </bean>
    
        <!--结合AOP实现事务织入-->
        <!--配置事务通知-->
        <tx:advice id="tx" transaction-manager="transactionManager">
            <tx:attributes>
                <!--给哪些方法配置事务-->
                <!--  配置事务传播特性 propagation="REQUIRED"-->
                <tx:method name="select*"></tx:method>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
        <!--配置事务切入-->
        <aop:config>
            <aop:pointcut id="txPointcut" expression="execution(* com.doubleh.mapper.*.*(..))"></aop:pointcut>
            <aop:advisor advice-ref="tx" pointcut-ref="txPointcut"></aop:advisor>
        </aop:config>
    </beans>

    2、声明式事务

    <!--结合AOP实现事务织入-->
        <!--配置事务通知-->
        <tx:advice id="tx" transaction-manager="transactionManager">
            <tx:attributes>
                <!--给哪些方法配置事务-->
                <!--  配置事务传播特性 propagation="REQUIRED"-->
                <tx:method name="select*"></tx:method>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
        <!--配置事务切入-->
        <aop:config>
            <aop:pointcut id="txPointcut" expression="execution(* com.doubleh.mapper.*.*(..))"></aop:pointcut>
            <aop:advisor advice-ref="tx" pointcut-ref="txPointcut"></aop:advisor>
        </aop:config>

    3、编程式事务(略)

  • 相关阅读:
    java.io.IOException: HTTPS hostname wrong: should be 规格严格
    linux syslog 规格严格
    SVN,HG,GIT 命令说明 规格严格
    pclose : no child process 规格严格
    使用NetBeans6开发OSGi应用(1)——FirstOSGi[88250原创]
    Netbeans大战Eclipse 谁将走向祭坛?
    XP中的重要惯例和规则
    使用NetBeans6开发OSGi应用(1)——FirstOSGi[88250原创]
    简简单单删除所有.svn目录
    简简单单删除所有.svn目录
  • 原文地址:https://www.cnblogs.com/xp2h/p/12380480.html
Copyright © 2020-2023  润新知