jar包:
aopalliance-1.0.jar commons-dbcp-1.4.jar commons-logging-1.2.jar commons-pool-1.6.jar mysql-connector-java-5.1.0-bin.jar spring-aop-4.3.9.RELEASE.jar spring-beans-4.3.9.RELEASE.jar spring-context-4.3.9.RELEASE.jar spring-core-4.3.9.RELEASE.jar spring-expression-4.3.9.RELEASE.jar spring-jdbc-4.2.0.RELEASE.jar spring-tx-4.3.9.RELEASE.jar创建AccountDao.java接口
package com.zy.dao; public interface AccountDao { /** * 汇款 * @param outer 汇款人 * @param money 汇款金额 */ public void out(String outer,int money); /** * 收款 * @param inner 收款人 * @param money 收款金额 */ public void in(String inner,int money); }创建AccountDaoImpl.java
package com.zy.dao.impl; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.zy.dao.AccountDao; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { /** * 根据用户名减少账户金额 */ @Override public void out(String outer, int money) { this.getJdbcTemplate().update("update account set money = money - ? where username = ?",money,outer); System.out.println(outer+"的钱-"+money); } /** * 根据用户名增加账户金额 */ @Override public void in(String inner, int money) { this.getJdbcTemplate().update("update account set money = money + ? where username = ?",money,inner); System.out.println(inner+"的钱+"+money); } }
创建AccountService.java接口
package com.zy.service; public interface AccountService { /** * 转账 * @param outer 汇款人 * @param inner 收款人 * @param money 交易金额 */ public void transfer(String outer,String inner,int money); }创建AccountServiceImpl.java实现
package com.zy.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.zy.dao.AccountDao; import com.zy.service.AccountService; @Transactional(propagation=Propagation.REQUIRED, isolation = Isolation.DEFAULT) @Service("accountService") public class AccountServiceImpl implements AccountService{ @Resource(name="accountDao") private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void transfer(String outer, String inner, int money) { accountDao.out(outer, money); // 当事务中出现异常,事务将会回滚 // int i = 1/0; accountDao.in(inner, money); } }创建test.java测试类
package com.zy.Test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zy.service.AccountService; public class test{ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = (AccountService) context.getBean("accountService"); //Tom 向 Marry 转账1000 accountService.transfer("Tom", "Marry", 1000); } /** * 采用注解方式测试类 不用写main()函数 @Test public void testNoTransaction(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService account = (AccountService) context.getBean("accountService"); //Tom 向 Marry 转账1000 account.transfer("Tom", "Marry", 1000); } */ }