码迷,mamicode.com
首页 > 编程语言 > 详细

MyBatis-Spring 事务配置

时间:2020-02-28 22:50:21      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:drive   session   mys   div   exec   com   url   instance   rac   

官方链接: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、编程式事务(略)

MyBatis-Spring 事务配置

标签:drive   session   mys   div   exec   com   url   instance   rac   

原文地址:https://www.cnblogs.com/xp2h/p/12380480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!