银行转账】"/>
【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实现模拟银行转账】
发布评论