## 配置
~~~
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
~~~
~~~
package com.like.domain;
import java.io.Serializable;
public class Account implements Serializable
{
private Integer id;
private String name;
private Float money;
@Override
public String toString()
{
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + 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;
}
}
~~~
~~~
package com.like.dao;
import com.like.domain.Account;
public interface IAccountDao
{
Account findAccountByName(String name);
void updateAccount(Account account);
}
~~~
~~~
package com.like.dao.impl;
import com.like.dao.IAccountDao;
import com.like.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class AccountDaoImpl implements IAccountDao
{
@Autowired
private JdbcTemplate jt;
public Account findAccountByName(String name)
{
List accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper(Account.class), name);
if (accounts.isEmpty()) {
return null;
}
if (accounts.size() > 1) {
throw new RuntimeException("结果集不唯一");
}
return accounts.get(0);
}
public void updateAccount(Account account)
{
jt.update("update account set money = ? where name = ?", account.getMoney(), account.getName());
}
}
~~~
~~~
package com.like.service;
public interface IAccountService
{
void transfer(String sourceName,String targetName,Float money);
}
~~~
~~~
package com.like.service.impl;
import com.like.dao.IAccountDao;
import com.like.domain.Account;
import com.like.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class AccountServiceImpl implements IAccountService
{
@Autowired
private IAccountDao accountDao;
public void transfer(String sourceName, String targetName, Float money)
{
Account source = accountDao.findAccountByName(sourceName);
Account target = accountDao.findAccountByName(targetName);
source.setMoney(source.getMoney() - money);
target.setMoney(target.getMoney() + money);
accountDao.updateAccount(source);
int a = 1/0;
accountDao.updateAccount(target);
}
}
~~~