Spring中操作事务的方式主要有两种:
编程式事务
声明式事务
1、编程式事务
使用编程的方式,自己去实现事务管理工作,例如事务的开启、提交、回滚操作,需要开发人员自己调用commit()或者rollback()等方法来实现。
编程式事务需要我们自己在逻辑代码中手动书写事务控制逻辑,所以编程式事务是具有侵入性的。我们之前的代码中,对事务进行提交或者回滚的操作,就属于编程式事务。
2、声明式事务
只需要声明或者配置一个事务就可以了,不需要我们手动去编写事务管理代码。
这种方式属于非侵入性的,可以使用AOP思想实现事务管理,能够提高代码的复用性,提高开发效率。
例如,在Spring中,使用AOP来实现事务操作
将service层事务的开启、提交、回滚等代码抽出来,封装成切面类
使用环绕通知,将事务管理的代码织入到service层需要事务支持的方法中
获取service 实现类的代理对象,调用方法时会动态加入事务管理的代码
事务的xml配置
dao层
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!-- 配置数据库的连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/jd2202?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="rootroot"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
<!-- 配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置dao层对象 -->
<bean id="teacherDao" class="com.briup.transaction.TeacherDao">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
<!-- 配置service层对象 -->
<bean id="teacherService" class="com.briup.transaction.TeacherServiceImpl">
<property name="teacherDao" ref="teacherDao"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务拦截器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--
配置事务属性
主要是五个方面
传播行为、隔离级别、是否只读、超时时间、回滚规则
-->
<tx:attributes>
<!--
name 要拦截的方法,可以使用*通配
read-only 是否只读,一般情况下,查询的方法设置为只读
propagation 设置传播行为,一共有七种
rollback-for 回滚规则,默认为error和runtimeException,一般设置为Exception大类
no-rollback-for 设置某个异常发生的时候事务不回滚,具体情况具体分析
timeout 设置事务超时时间
-->
<tx:method name="save*" read-only="true" propagation="SUPPORTS"></tx:method>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置aop,将事务管理的代码织入到切入点里 -->
<aop:config>
<!-- 定义切入点 -->
<aop:pointcut id="pointcut" expression="execution(* com.briup.transaction.ITeacherService.*())"></aop:pointcut>
<!-- 将事务拦截器和切入点组合在一起 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
## Spring,mybatis的xml
```java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/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">
<!-- dao层主要配置数据源和mapper对象 -->
<!-- 读取properties配置文件中的数据 -->
<context:property-placeholder location="classpath:datasource.properties"/>
<!-- 配置数据库的连接 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 设置别名 -->
<property name="typeAliasesPackage" value="com.briup.jdbc"/>
<!-- 注册mapper接口 -->
<property name="mapperLocations" value="classpath:com/briup/mybatis/*Mapper.xml"/>
</bean>
<!-- 配置映射接口所在位置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.briup.mybatis"/>
</bean>
</beans>
service层
<?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">
<!-- 配置service层的实现类 -->
<bean id="teacherService" class="com.briup.mybatis.TeacherImpl">
<property name="mapper" ref="teacherMapper"/>
</bean>
<!-- 配置事务管理器,可以拿着上午的代码直接复制 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务拦截器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" read-only="true" propagation="SUPPORTS"></tx:method>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置aop,将事务管理的代码织入到切入点里 -->
<aop:config>
<!-- 定义切入点 -->
<aop:pointcut id="pointcut" expression="execution(* com.briup.transaction.ITeacherService.*())"></aop:pointcut>
<!-- 将事务拦截器和切入点组合在一起 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>