官方链接:http://mybatis.org/spring/zh/transactions.html#configuration
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&useUnicode=true&characterEncoding=utf-8&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>
<!--结合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>
原文:https://www.cnblogs.com/xp2h/p/12380480.html