JAVAEE基础-实验07:Spring事务管理(补充)

实验目的:
熟练掌握声明式事务管理。
实验内容:
编写一个模拟银行转账的程序,要求在转账时通过Spring对事务进行控制。
实验要求:
(1) 分别使用基于XML和基于注解的声明式事务管理方式来实现。
(2)提交源代码和运行截图。

首先建立项目如图:
需要的spring包(有多余的我也不知道哪些多余反正加都加了嘻嘻)项目结构目录
表结构
1.com.account.service里的ServiceDao和ServiceDaoImpl

package com.account.service;
		//接口
       public interface ServiceDao {
      	 public void transfer(String account_id_out,String account_id_in,int balance);
       }

package com.account.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service("serviceDao")
//实现接口
public class ServiceDaoImpl implements ServiceDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public void transfer(String account_id_out, String account_id_in, int balance) {
		String sql = "update accounts set balance=balance-? where account_id=?";
		String sql2 = "update accounts set balance=balance+? where account_id=?";
		jdbcTemplate.update(sql, balance, account_id_out);
		jdbcTemplate.update(sql2, balance, account_id_in);
	}
}

2.com.account.controller

package com.account.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.account.service.ServiceDao;

@Controller("accountController")
public class AccountController {
	@Autowired
	private ServiceDao serviceDao;
	public String test() {
		String message;
		try {
			serviceDao.transfer(
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值