(开心娱乐一下)
前面我们讲解了通过注解方法对类或方法添加事务,式它们可以被执行时不会因为一些异常导致数据产生问题,在我们的开发过程中注解方式是最常用的,但是配置文件也需要进行掌握知道如何通过配置文件方式实现事务操作,上一篇文章讲解了事务操作的一些参数,如果没有了解可以去我主页Spring专栏了解,因为下面我们将通过配置方式再次实现那些事务参数。
之前我们通过注解方式,解决银行转账业务实现:用户一向用户二转账,但是由于一些异常问题导致用户一数据库中的money值减少了,但是用户二的money值却没有增加,同样我们本篇文章也是以此为例,通过配置文件方式实现异常回滚。
案例准备:
1:在MySQL中创建一个表,模拟银行后台数据库
2.创建类实现转账业务
@Service
public class operat_system {
@Autowired
private JdbcTemplate jdbcTemplate;
public void changmoney(Object[] user,int id){ //实现银行取钱和存钱功能,在一表中的money值发生变化
if (id==1){
String sql ="UPDATE myemployees.`bank` SET myemployees.`bank`.`money`=myemployees.`bank`.`money`-? WHERE myemployees.`bank`.`id`=?";
jdbcTemplate.update(sql,user);
System.out.println("取钱成功");
}
else{
String sql ="UPDATE myemployees.`bank` SET myemployees.`bank`.`money`=myemployees.`bank`.`money`+? WHERE myemployees.`bank`.`id`=?";
jdbcTemplate.update(sql,user);
System.out.println("存钱成功");
}
}
public void zz(operat_system aa,Object[] user,Object[] user2){ //实现银行用户的转账功能,从用户一转到用户二
aa.changmoney(user,1);
int m = 10/0; //模拟异常出现
aa.changmoney(user2,0);
System.out.println("转账成功");
}
}
3.配置XML文件(配置事务参数,实现事务的各种操作)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql://127.0.0.1:3306" />
<property name="username" value="demo1" />
<property name="password" value="123" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean>
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<context:component-scan base-package="new_study.bank_system"></context:component-scan>
<!--创建事务管理器-->
<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置通知,对方法添加事务操作,并添加到事务管理器-->
<tx:advice id="txadv" transaction-manager="tm">
<tx:attributes>
<!-- 为zz方法添加事务传播行为-->
<tx:method name="zz" propagation="REQUIRED"/>
<!-- 为zz方法添加事务隔离-->
<tx:method name="zz" isolation="SERIALIZABLE"></tx:method>
<!-- 为zz方法设置事务超时时间-->
<tx:method name="zz" timeout="-1"></tx:method>
<!-- 为zz方法设置只读操作-->
<tx:method name="zz" read-only="false"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置切入点和切面-->
<aop:config>
<!-- 配置切入点,*(..)是指operat_system类中的所有方法-->
<aop:pointcut id="point" expression="execution(* new_study.bank_system.operat_system.*(..))"/>
<!-- 配置切面-->
<aop:advisor advice-ref="txadv" pointcut-ref="point"></aop:advisor>
</aop:config>
</beans>
4:测试类:
package new_study.bank_system;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("new_study/bank_system/bean2.xml");
operat_system bank1 = context.getBean("operat_system", operat_system.class);
Object[] a1 = {100, "1"};
Object[] a2 = {100, "2"};
}
}
输出结果:
只执行了第一个方法,然后遇见了异常
看一下数据库中的数据
执行了第一个方法,但是数据库中的数据并没有发生改变,说明操作进行了回滚
以上就是配置文件配置的事务操作
后期会不断地更新关于Spring5框架的知识