spring整合mybatis实现配置AOP方式的事务
-
事务要么都成功,要么都失败
-
确保完整性和一致性
acid
-
原子性,一致性:资源状态一致。隔离性:多个业务可以操作同一个资源,不会被影响。持久性,事务一旦提交,被保存,不会被改动
-
如果不配置事务,执行两个操作前一个成功后一个失败,那么不会回滚前一个成功的,会对数据库造成数据不一致的问题
比如执行前:
-
执行:
-
执行后:会报错
-
但是上面的id为2的user也被删除了
-
所以要加入事务,要成功一起成功,要失败一起失败
添加事务后:
-
id为2的记录还在
-
spring配置事务:
步骤:
-
添加tx标签约束
-
注入事务管理的bean使用其中的构造方法传入DataSource
-
配置事务通知:哪些sql需要使用事务,注意:method name=“delete*” 是方法名
-
使用切面,配置切面
-
配置切点
-
设置切点使用什么通知
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--导入外部数据库配置文件:多个文件使用逗号隔开,同属性名,取加载在最后的那个属性-->
<context:property-placeholder location="classpath:db.properties" file-encoding="UTF-8"/>
<!--使用spring的数据源替换mybatis的配置 c3p0 dbcp
使用Spring提供的jdbc:org.springframework.jdbc.dataSource.DriverManagerDataSource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbcDriver}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${jdbcUsername}"/>
<property name="password" value="${jdbcPassword}"/>
</bean>
<!--注册sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定Mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:dao/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--注入sqlFactory,无set方法,查看SqlSessionTemplate原码-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--开启事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
<!--结合Aop实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务的传播特性 propagation 默认REQUIRED-->
<tx:attributes>
<!-- <tx:method name="add*" propagation="REQUIRED"/>-->
<!-- <tx:method name="delete*"/>-->
<!-- <tx:method name="update*"/>-->
<!-- <tx:method name="insert*"/>-->
<!-- <tx:method name="query*" read-only="true"/>-->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/>-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
```