spring编写事务小实例———转账

一.境搭建

1.创建数据库:

在这里插入图片描述

2.导入jar包:

在这里插入图片描述

3.Dao层代码:

a.DaoAccount接口:
转账方法:in代表转入,out代表转出。

package com.fxy.studyspring.Dao;

public interface AccountDao {
	public void in(String inner,int money);
	public void out(String outer,int money);	
}

b.实现类:
实现类注入继承JdbcSupport类获取JdbcTemplate模板。

package com.fxy.studyspring.daoimpl;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.fxy.studyspring.Dao.AccountDao;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{

	@Override
	public void in(String inner, int money) {
		this.getJdbcTemplate().update("update fxyaccount set money=money+? where username=?", money,inner);
	}

	@Override
	public void out(String outer, int money) {
		this.getJdbcTemplate().update("update fxyaccount set money=money-? where username=?", money,outer);
	}

}
4.service层代码:

a.accountService接口:
transform表示转账方法,传入转账者,收入者和转账钱数。

package com.fxy.studyspring.service;

public interface accountService {

	public void transform(String inner,String outer,int money);
}

b.实现类代码:
这里需要通过set方法将Dao注入进去,以使用dao层来操作数据库内容。

package com.fxy.studyspring.serviceimpl;

import com.fxy.studyspring.Dao.AccountDao;
import com.fxy.studyspring.service.accountService;

public class accountServiceImpl implements accountService{
	
	AccountDao account;
	
	public void setAccount(AccountDao account) {
		this.account = account;
	}
	@Override
	public void transform(String inner, String outer, int money) {
		// TODO Auto-generated method stub
		account.in(inner, money);
		account.out(outer, money);
	}

}

5.配置文件:

a.

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-4.2.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
	<!-- 加载properties文件 -->
	<context:property-placeholder location="classpath:properties.properties"/>
	
	<!-- 获取数据源 ,配置数据库的连接-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>		
		<property name="user" value="${jdbc.user}"></property>		
		<property name="password" value="${jdbc.password}"></property>
	</bean>					
	
	<!-- 获取Dao,将数据源注入到Dao,会自动创建模板-->
	<bean id="accountDao" class="com.fxy.studyspring.daoimpl.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 获取service,将Dao注入到service -->
	<bean id="accountService" class="com.fxy.studyspring.serviceimpl.accountServiceImpl">
		<property name="account" ref="accountDao"></property>
	</bean>
</beans>

b.数据库连接properties文件:

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/account?useSSL=false&serverTimezone=UTC
jdbc.user=root
jdbc.password=123456
6.测试文件:
package com.fxy.studyspring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fxy.studyspring.service.accountService;

public class TestAccount {
	@Test
	public void demo01() {
		String xmlPath = "applicationContext.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		accountService service = (accountService) applicationContext.getBean("accountService");
		service.transform("kqy", "fxy", 1000);
	}
}

通过测试可以发现,当程序正常运行时,可以正确的对转账操作进行操作,可是当在转账过程中发生了异常情况,就会出现各种问题,转账可能就不能顺利完成,所以这个时候我们需要引入事务操作。

二.手动方式获取事务管理:基于上面的环境配置

1.手动方式需要获得事务模板,然后将事务模板注入到service层中,而事务模板需要跟事务管理器挂钩,所以我们也需要获得事务管理器。同时需要改正service层代码。
2.service层需要获取事务模板,所以我们需要通过set方法设置事务模板:通过事务模板调用execut()方法,然后通过匿名内部类实现TransactionCallbackWithoutResult重写doInTransactionWithoutResult方法,将转账代码写在这个方法中。
package com.fxy.studyspring.serviceimpl;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.fxy.studyspring.Dao.AccountDao;
import com.fxy.studyspring.service.accountService;

public class accountServiceImpl implements accountService{
	
	AccountDao account;
	
	public void setAccount(AccountDao account) {
		this.account = account;
	}
	
	TransactionTemplate transactiontemplate;
	
	public void setTransactiontemplate(TransactionTemplate transactiontemplate) {
		this.transactiontemplate = transactiontemplate;
	}

	@Override
	public void transform(String inner, String outer, int money) {
		transactiontemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				account.in(inner, money);
//				int i=1/0;
				account.out(outer, money);
			}
		});

	}

}
3.配置文件,需要注意的是

获取事务管理器,需要获取事务,事务是从connection中获取,所以需要将连接(数据源)注入其中

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-4.2.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
	
	<context:property-placeholder location="classpath:properties.properties"/>
	
	<!-- 获取数据源 ,配置数据库的连接-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>		
		<property name="user" value="${jdbc.user}"></property>		
		<property name="password" value="${jdbc.password}"></property>
	</bean>					
	
	<!-- 获取Dao,将数据源注入到Dao,会自动创建模板-->
	<bean id="accountDao" class="com.fxy.studyspring.daoimpl.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 获取service,将Dao注入到service,需要将事务模板注入其中-->
	<bean id="accountService" class="com.fxy.studyspring.serviceimpl.accountServiceImpl">
		<property name="account" ref="accountDao"></property>
		<property name="transactiontemplate" ref="transactionTemplate"></property>
	</bean>
	<!-- 获取事务模板,需要将事务管理器注入其中 -->
	<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="txManager"></property>
	</bean>
	<!-- 获取事务管理器,需要获取事务,事务是从connection中获取,所以需要将连接(数据源)注入其中 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

其他文件代码与一相同,不用做改动。

三.半自动,需要获取代理对象,然后通过代理对象调用转账方法。

1.这里我们需要获取事务代理类:工厂bean事务的代理,TransactionProxyFactoryBean
然后在这个类中设置事务的属性

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-4.2.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
	
	<context:property-placeholder location="classpath:properties.properties"/>
	
	<!-- 获取数据源 ,配置数据库的连接-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>		
		<property name="user" value="${jdbc.user}"></property>		
		<property name="password" value="${jdbc.password}"></property>
	</bean>					
	
	<!-- 获取Dao,将数据源注入到Dao,会自动创建模板-->
	<bean id="accountDao" class="com.fxy.studyspring.daoimpl.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 获取service,将Dao注入到service -->
	<bean id="accountService" class="com.fxy.studyspring.serviceimpl.accountServiceImpl">
		<property name="account" ref="accountDao"></property>
	</bean>
	
	<!-- 配置 代理
		需要4项
		1.代理类接口
		2.代理类
		3.事务管理器
		4.事务属性
	-->
	<bean id="proxyAccount" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="target" ref="accountService"></property><!-- 代理的目标类 -->
		<property name="proxyInterfaces" value="com.fxy.studyspring.service.accountService"></property><!-- 代理的目标类接口 -->
		<property name="transactionManager" ref="txManager"></property><!-- 跟事务管理器挂钩 -->
		<property name="transactionAttributes"><!-- 设置事务的属性 -->
			<props>
				<prop key="transform">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop><!-- 事务所管理的方法, -->
			</props>
		</property>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

2.测试文件中获取的是代理bean

package com.fxy.studyspring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fxy.studyspring.service.accountService;

public class TestAccount {
	@Test
	public void demo01() {
		String xmlPath = "applicationContext.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		accountService service = (accountService) applicationContext.getBean("proxyAccount");//这里获取的是代理bean
		service.transform("kqy", "fxy", 1000);
	}
}

其他的代码文件与一中的一样,无需做改动。

四.基于xml文件的全自动,不需要手动获取代理,交给spring去做。

1.这里只有配置文件需要改动,剩下的和一中相同。

这里利用AOP编程,事务为通知,service代码为切入点。

2.几点说明:tx:advice 事务通知,需要和事务管理器挂钩,tx:attributes,事务属性,在这里配置事务的各种属性。 <tx:method name=“transform” propagation=“REQUIRED” isolation=“DEFAULT”/>需要增强的方法。propagation传播行为为默认方式,隔离级别为默认。
<?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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-4.2.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	
	<context:property-placeholder location="classpath:properties.properties"/>
	
	<!-- 获取数据源 ,配置数据库的连接-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>		
		<property name="user" value="${jdbc.user}"></property>		
		<property name="password" value="${jdbc.password}"></property>
	</bean>					
	
	<!-- 获取Dao,将数据源注入到Dao,会自动创建模板-->
	<bean id="accountDao" class="com.fxy.studyspring.daoimpl.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 获取service,将Dao注入到service -->
	<bean id="accountService" class="com.fxy.studyspring.serviceimpl.accountServiceImpl">
		<property name="account" ref="accountDao"></property>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 事务属性,即通知 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="transform" propagation="REQUIRED" isolation="DEFAULT"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- AOP编程,配置切入点,和通知引用 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.fxy.studyspring..*.*(..) )"/>
	</aop:config>
</beans>

五.基于注解的全自动方式:

1.<tx:annotation-driven transaction-manager=“txManager”/>,可以理解为注解驱动,同样的也需要和事务管理器挂钩。
<?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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-4.2.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	
	<context:property-placeholder location="classpath:properties.properties"/>
	
	<!-- 获取数据源 ,配置数据库的连接-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>		
		<property name="user" value="${jdbc.user}"></property>		
		<property name="password" value="${jdbc.password}"></property>
	</bean>					
	
	<!-- 获取Dao,将数据源注入到Dao,会自动创建模板-->
	<bean id="accountDao" class="com.fxy.studyspring.daoimpl.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 获取service,将Dao注入到service -->
	<bean id="accountService" class="com.fxy.studyspring.serviceimpl.accountServiceImpl">
		<property name="account" ref="accountDao"></property>
	</bean>
	
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<tx:annotation-driven transaction-manager="txManager"/>
</beans>
2.添加注解:@Transactional(),可以在括号里面配置相关属性。
package com.fxy.studyspring.serviceimpl;

import org.springframework.transaction.annotation.Transactional;

import com.fxy.studyspring.Dao.AccountDao;
import com.fxy.studyspring.service.accountService;

@Transactional()
public class accountServiceImpl implements accountService{
	
	AccountDao account;
	
	public void setAccount(AccountDao account) {
		this.account = account;
	}
	@Override
	public void transform(String inner, String outer, int money) {
		// TODO Auto-generated method stub
		account.in(inner, money);
		int i = 1/0;
		account.out(outer, money);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值