1 DAO
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
@Override
public Account findAccountById ( Integer accountId) {
List< Account> accounts = super . getJdbcTemplate ( ) . query ( "select * from account where id = ?" , new BeanPropertyRowMapper < Account> ( Account. class ) , accountId) ;
return accounts. isEmpty ( ) ? null: accounts. get ( 0 ) ;
}
@Override
public Account findAccountByName ( String accountName) {
List< Account> accounts = super . getJdbcTemplate ( ) . query ( "select * from account 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) {
super . getJdbcTemplate ( ) . update ( "update account set name=?,money=? where id=?" , account. getName ( ) , account. getMoney ( ) , account. getId ( ) ) ;
}
}
2 Service
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate ( TransactionTemplate transactionTemplate) {
this . transactionTemplate = transactionTemplate;
}
public void setAccountDao ( IAccountDao accountDao) {
this . accountDao = accountDao;
}
@Override
public Account findAccountById ( final Integer accountId) {
return transactionTemplate. execute ( new TransactionCallback < Account> ( ) {
@Override
public Account doInTransaction ( TransactionStatus transactionStatus) {
return accountDao. findAccountById ( accountId) ;
}
} ) ;
}
@Override
public void transfer ( final String sourceName, final String targetName, final Float money) {
transactionTemplate. execute ( new TransactionCallback < Object> ( ) {
@Override
public Object doInTransaction ( TransactionStatus transactionStatus) {
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) ;
int i = 1 / 0 ;
accountDao. updateAccount ( target) ;
return null;
}
} ) ;
}
}
3 bean.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"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: context= " http://www.springframework.org/schema/context"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
< bean id = " accountService" class = " com.tzb.service.impl.AccountServiceImpl" >
< property name = " accountDao" ref = " accountDao" > </ property>
< property name = " transactionTemplate" ref = " transactionTemplate" > </ property>
</ bean>
< bean id = " accountDao" class = " com.tzb.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/spring5?userSSL=false" > </ property>
< property name = " username" value = " root" > </ property>
< property name = " password" value = " root" > </ property>
</ bean>
< bean id = " txManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< bean id = " transactionTemplate" class = " org.springframework.transaction.support.TransactionTemplate" >
< property name = " transactionManager" ref = " txManager" > </ property>
</ bean>
</ beans>
4 單元測試
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( locations = "classpath:bean.xml" )
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testTransfer ( ) {
as. transfer ( "Mike" , "Jenny" , 100f ) ;
}
}