关闭

spring编程式事务管理的环境搭建及实例

标签: string管理编程事务
118人阅读 评论(0) 收藏 举报
分类:

以银行转账为例(本文只作为自己学习spring的备忘录,只对自己负责)

转账的dao层接口

package com.slowly.springtrancation01;

/**
 * 转账的dao层接口
 * @author hy
 *
 */
public interface accountDao {
	/**
	 * 
	 * @param out 转出账户
	 * @param money 转出金额
	 */
	public void outMoney(String out,Double money);
	
	/**
	 * 
	 * @param in 转入账户
	 * @param money 转入金额
	 */
	public void inMoney(String in,Double money);
}

转账dao层接口实现类

package com.slowly.springtrancation01;

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

public class accountDaoImp extends JdbcDaoSupport implements accountDao {

	@Override
	public void outMoney(String out, Double money) {
		String sql = "update account set balance = balance-? where account_name = ?";
		this.getJdbcTemplate().update(sql, money,out);

	}

	@Override
	public void inMoney(String in, Double money) {
		String sql = "update account set balance = balance+? where account_name = ?";
		this.getJdbcTemplate().update(sql, money,in);

	}

}


转账的业务逻辑层接口

package com.slowly.springtrancation01;
/*
 * 转账的业务层的接口
 */
public interface accountService {
	/**
	 * 
	 * @param out 转出账户
	 * @param in 转入账户
	 * @param money 转入的金额
	 */
	public void transfer(String out,String in,Double money);
}

业务逻辑接口实现类

package com.slowly.springtrancation01;

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

public class accountServiceImp implements accountService {
	
	private accountDao accountDao;
	
	

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

	private TransactionTemplate transationTemplate;
	
	

	public void setTransationTemplate(TransactionTemplate transationTemplate) {
		this.transationTemplate = transationTemplate;
	}



	@Override
	public void transfer(String out, String in, Double money) {
		transationTemplate.execute(new TransactionCallbackWithoutResult() {//利用tranactionTempalte管理事务
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				accountDao.outMoney(out, money);
				//int i = 1/0;
				accountDao.inMoney(in, money);

				
			}
		});
	
	}

}


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: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.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">
        
                
        
  	<!-- 引入外部的文件 -->      
      <context:property-placeholder location="classpath:jdbc.properties"/>
        <!-- 配置c3p0的连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        	<property name="driverClass" value="${driverClass}"></property>
        	<property name="jdbcUrl" value="${jdbcUrl}"></property>
        	<property name="user" value="${user}"></property>
        	<property name="password" value="${password}"></property>
        </bean>
        <!-- 配置业务层 -->
        <bean id="accountService" class="com.slowly.springtrancation01.accountServiceImp">
        	<property name="accountDao" ref="accountDao"></property>
        	<property name="transationTemplate" ref="transactionTemplate"></property>
        </bean>
        
        <!-- 配置dao层 -->
        <bean id="accountDao" class="com.slowly.springtrancation01.accountDaoImp">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 事务管理配置 -->
        <bean id="tranctionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 配置事务模板 -->
        <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        	<property name="transactionManager" ref="tranctionManager"></property>
        </bean>
</beans>

测试

package com.slowly.springtrancation01;

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

public class Test {

	public static void main(String[] args) {
		ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		accountService accountService = (com.slowly.springtrancation01.accountService) apt.getBean("accountService");
		accountService.transfer("乙", "甲", 600.00);

	}

}


工程



account变得创建参考http://blog.csdn.net/hanxueyu666/article/details/53733652中account表的创建,两处用的是一个表


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:66628次
    • 积分:1625
    • 等级:
    • 排名:千里之外
    • 原创:88篇
    • 转载:74篇
    • 译文:0篇
    • 评论:9条
    个人声明
    本博客的主要作为自己的学习笔记,并不是以教授知识为目的。如果有人能从我的博客中学到东西,我很高兴。有人发现文章中有不对的地方,也欢迎大家纠正。
    最新评论