【Spring实现模拟银行转账】

数据库建表和插入数据

1、数据库建表

2、插入数据

项目目录

代码部分

1、IAccountDao.java

package com.qingruan.dao;

public interface IAccountDao {
    public void addMoney(String name,double money);
    public void subMoney(String name,double money);

}

2、AccountDaoImpl.java

package com.qingruan.dao.impl;

import com.qingruan.dao.IAccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements IAccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addMoney(String name, double money) {
        String sql="update account set money = money + ? where name = ?";
        jdbcTemplate.update(sql,money,name);
    }

    @Override
    public void subMoney(String name, double money) {
        String sql="update account set money = money - ? where name = ?";
        jdbcTemplate.update(sql,money,name);
    }
}

3、IAccountService.java

package com.qingruan.service;


public interface IAccountService {
    public void transfer(String form,String target,double money);
}

4、AccountServiceImpl.java

package com.qingruan.service.impl;

import com.qingruan.dao.IAccountDao;
import com.qingruan.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao;

    @Override
    public void transfer(String form, String target, double money) {
        accountDao.subMoney(form,money);
        //System.out.println(1/0);
        accountDao.addMoney(target,money);
    }
}

5、TestApp.java

package com.qingruan.test;

import com.qingruan.service.IAccountService;
import com.qingruan.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

    @Test
    public void test(){
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountServiceImpl service = (AccountServiceImpl)app.getBean(IAccountService.class);
        service.transfer("李四","张三",500);
    }
}

配置文件

<
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值