spring tx 事务管理基于xml实现

本文详细介绍使用Spring框架通过XML配置实现AOP自动代理,进行事务管理的具体实践。包括配置事务管理器、定义事务规则、配置AOP切面,以及通过DAO层、Service层接口和实现展示事务操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

事务管理的实现在spring xml 配置aop 自动生成代理,进行事务的管理
1.配置管理器
2.配置事务详情
3.配置aop

代码结构
在这里插入图片描述
Dao层接口类

package com.qst.b_tx;

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

接口实现

package com.qst.b_tx;

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

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

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

	}

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

	}

}

service接口

package com.qst.b_tx;

public interface AccountService {
	public void transfer(String outer,String inner, Integer money);
}

接口实现

package com.qst.b_tx;

public class AccountServiceImpl implements AccountService{

	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	@Override
	public void transfer(String outer, String inner, Integer money) {
			accountDao.out(outer, money);
			accountDao.in(inner, money);

		
	}
	
}

配置文件

package com.qst.b_tx;

public class AccountServiceImpl implements AccountService{

	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	@Override
	public void transfer(String outer, String inner, Integer money) {
			accountDao.out(outer, money);
			accountDao.in(inner, money);

		
	}
	
}

测试类

package com.qst.b_tx;

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

public class test {
	public static void main(String[] args) {
		String xmlPath = "com/qst/b_tx/applicationContext.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		AccountService accountService =(AccountService) applicationContext.getBean("accountService");
		accountService.transfer("jack", "rose", 1000);
	}
}

数据库创建代码

create database ee19_spring_day03;
use ee19_spring_day03;
create table account(
  id int primary key auto_increment,
  username varchar(50),
  money int
);
insert into account(username,money) values('jack','10000');
insert into account(username,money) values('rose','10000');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值