欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring小例子转账

程序员文章站 2022-04-09 09:14:54
...

代码实现:

1.数据层

public class AccountDao extends JdbcDaoSupport{
	//
	public void addMoney(String name,Double money) {
		String sql = "update bank set money = money + ? where name = ?";
		this.getJdbcTemplate().update(sql,money,name);
		
	}
	public void reduceMoney(String name,Double money) {
		String sql = "update bank set money = money - ? where name = ?";
		this.getJdbcTemplate().update(sql,money,name);
	}
}


2.服务层

public class AccountService {
	/*
	private AccountDao accountDao ;
	private TransactionTemplate template;
	
	public void setTemplate(TransactionTemplate template) {
		this.template = template;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public void transfer(final String add,final String reduce,final Double money) {
		TransactionCallback tc = new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				accountDao.addMoney(add, money);
				accountDao.reduceMoney(reduce, money);
				
			}
		};
		template.execute(tc);
	}*/
	//上面的与下面代码相同效果,下面使用的是bean注入
	private AccountDao accountDao ;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	public Boolean transfer(final String add,final String reduce,final Double money) {
		accountDao.addMoney(add, money);
		accountDao.reduceMoney(reduce, money);
		return true;
	}
}

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:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd
		http://www.springframework.org/schema/util 
		http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!--事务对应的通知类定义  -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 定义事务相关的信息 -->
		<tx:attributes>
			<!--哪些方法带有事务必须告诉切面  -->
			<tx:method 
			name="transfer"
			timeout="-1"
			read-only="false"
			isolation="DEFAULT"
			propagation="REQUIRED"
			
			/>
			<!-- <tx:method name="get*" ....这里单独定义属性 get开头方法的属性/> -->
			<!-- <tx:method name="*" 给所有 通知类的方法 加属性/> -->
			
			<!--
			timeout="-1":超时设置
			isolation="DEFAULT" :隔离度的级别
			no-rollback-for="异常类名":遇到这个异常类不回滚,会继续执行
			rollback-for="异常类名" 遇到这个异常类回滚,不会继续执行
			propagation="REQUIRED":事物的传播属性
			  -->
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*.transfer(..))"/>
	</aop:config>
	
	<bean id="accountService" class="com.array.tx.AccountService">
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	<bean id="accountDao" class="com.array.tx.AccountDao">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- transactionManager -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--jdbc dataSource  -->
	<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
		<property name="username" value="array"></property>
		<property name="password" value="array"></property>
	</bean>
	
	

事务传播行为

Spring小例子转账

4.测试类

public class App {
	public static void main(String[] args) {
		ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext2.xml");
		AccountService accountService = (AccountService) act.getBean("accountService");
		Boolean boolean1 = accountService.transfer("object", "array", 100.00);
		if (boolean1) {
			System.out.println("转账成功");
		}
		
	}
}