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);
- }
- }