【Spring实现模拟银行转账】

编程入门 行业动态 更新时间:2024-10-09 22:23:01

【Spring实现模拟<a href=https://www.elefans.com/category/jswz/34/1770221.html style=银行转账】"/>

【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 {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void addMoney(String name, double money) {String sql="update account set money = money + ? where name = ?";jdbcTemplate.update(sql,money,name);}@Overridepublic 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 {@Autowiredprivate IAccountDao accountDao;@Overridepublic 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 {@Testpublic void test(){ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");AccountServiceImpl service = (AccountServiceImpl)app.getBean(IAccountService.class);service.transfer("李四","张三",500);}
}

配置文件

<

更多推荐

【Spring实现模拟银行转账】

本文发布于:2024-02-05 17:07:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1745464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:银行   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!