dao层:
package com.fy.dao;
import com.fy.domain.Account;
/**
* 账户持久层接口
*/
public interface AccountDao{
/**
* 根据Id的查询
* @param accountId
* @return
*/
Account findAccountById(Integer accountId);
/**
* 根据name查找
* @param accountName
* @return
*/
Account findAccountByName(String accountName);
/**
* 更改账户
* @param account
*/
void updateAccount(Account account);
}
package com.fy.dao.impl;
import com.fy.dao.AccountDao;
import com.fy.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
/*
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jabcTemplate) {
this.jdbcTemplate = jabcTemplate;
}
*/
@Override
public Account findAccountById(Integer accountId) {
List<Account> accounts = getJdbcTemplate().query("select * from account.demo1 where id=?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
}
@Override
public Account findAccountByName(String accountName) {
List<Account> accounts = getJdbcTemplate().query("select * from account.demo1 where name=?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
if(accounts.isEmpty()){
return null;
}
if(accounts.size()>1){
throw new RuntimeException("结果不唯一");
}
return accounts.get(0);
}
@Override
public void updateAccount(Account account) {
getJdbcTemplate().update("update account.demo1 set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}
domain:
package com.fy.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
service:
package com.fy.service;
import com.fy.dao.AccountDao;
import com.fy.domain.Account;
public interface AccountService {
/**
* 模拟查找账户
*/
Account finAccountById(Integer accountId);
/**
* 模拟转账
*/
void transfer(String sourceName, String targetName,Float money);
}
package com.fy.service.impl;
import com.fy.dao.AccountDao;
import com.fy.domain.Account;
import com.fy.service.AccountService;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public Account finAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Override
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer.....");
//根据名称查询转出账户
Account source = accountDao.findAccountByName(sourceName);
//根据名称查询转入账户
Account target = accountDao.findAccountByName(targetName);
//转出账户减钱
source.setMoney(source.getMoney()-money);
//转入账户加钱
target.setMoney(target.getMoney()+money);
//跟新转出账户
accountDao.updateAccount(source);
accountDao.updateAccount(target);
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountService" class="com.fy.service.impl.AccountServiceImpl">
<property name="accountDao" ref="AccountDao"></property>
</bean>
<bean id="AccountDao" class="com.fy.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/account?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="*****"></property>
</bean>
</beans>
测试类:
package com.fy.test;
import com.fy.domain.Account;
import com.fy.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:TXBean.xml")
public class AccountServiceTest {
@Autowired
private AccountService as;
@Test
public void testTransfer(){
as.transfer("aaa","bbb",100f);
}
}
运行前图片:
运行后图片: