Spring模拟转账开发

完成转账代码的编写
service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void pay(String out, String in, Double money) {
        accountDao.outMoney(out,money);
        accountDao.inMoney(in,money);
    }
}
dao
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void outMoney(String out, double money) {
        jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);
    }

    public void inMoney(String in, double money) {
        jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);
    }
}
配置文件





    
    
    
    



    



    



    
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo1 {
    @Autowired
    private AccountService accountService;
    @Test
    public void run1(){
        accountService.pay("熊大","熊二",10.0);
    }
}
Dao编写的方式(第二种方式)
service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void pay(String out, String in, Double money) {
        accountDao.outMoney(out,money);
        accountDao.inMoney(in,money);
    }
}
dao
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{

    //付款
    public void outMoney(String out, double money) {
        this.getJdbcTemplate().update("update account set money = money-? where name = ?",money,out);
    }

    public void inMoney(String in, double money) {
        this.getJdbcTemplate().update("update account set money = money + ? where name = ?",money,in);
    }
}
配置文件编写





    
    
    
    



    



    
测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo1 {
    @Autowired
    private AccountService accountService;
    @Test
    public void run1(){
        accountService.pay("熊大","熊二",10.0);
    }
}

    你可能感兴趣的:(spring框架,java,spring)