Dao
package com. itheima. dao. impl ;
import com. itheima. dao. IAccountDao ;
import com. itheima. 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 javax. sql. DataSource ;
import java. util. List ;
@Repository ( "accountDao" )
public class AccountDaoImpl implements IAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Account findAccountById ( Integer accountId) {
List < Account > accounts = jdbcTemplate. 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 = jdbcTemplate. query ( "select * from account where name = ?" , new BeanPropertyRowMapper < Account > ( Account . class ) , accountName) ;
if ( accounts. isEmpty ( ) || accounts== null ) {
return null ;
}
if ( accounts. size ( ) > 1 ) {
throw new RuntimeException ( "结果集不唯一" ) ;
}
return accounts. get ( 0 ) ;
}
@Override
public void updateAccountByName ( Account account) {
jdbcTemplate. update ( "update account set name=?,money=? where id = ? " ,
account. getName ( ) , account. getMoney ( ) , account. getId ( ) ) ;
}
}
Service
package com. itheima. service. impl ;
import com. itheima. dao. IAccountDao ;
import com. itheima. domain. Account ;
import com. itheima. service. IAccountService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import org. springframework. transaction. annotation. Propagation ;
import org. springframework. transaction. annotation. Transactional ;
import java. util. List ;
@Service ( "accountService" )
@Transactional ( propagation = Propagation . SUPPORTS , readOnly = true )
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Transactional ( propagation = Propagation . REQUIRED , readOnly = false )
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. updateAccountByName ( source) ;
int i= 1 / 0 ;
accountDao. updateAccountByName ( target) ;
}
}
config
package config ;
import org. springframework. context. annotation. ComponentScan ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. context. annotation. Import ;
import org. springframework. context. annotation. PropertySource ;
import org. springframework. transaction. annotation. EnableTransactionManagement ;
@Configuration
@ComponentScan ( "com.itheima" )
@Import ( { JdbcConfig . class , TransactionConfig . class } )
@PropertySource ( "classpath:jdbcConfig.properties" )
@EnableTransactionManagement
public class SpringConfiguration {
}
package config ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. context. annotation. Bean ;
import org. springframework. jdbc. core. JdbcTemplate ;
import org. springframework. jdbc. datasource. DriverManagerDataSource ;
import javax. sql. DataSource ;
public class JdbcConfig {
@Value ( "${jdbc.driver}" )
private String driver;
@Value ( "${jdbc.url}" )
private String url;
@Value ( "${jdbc.username}" )
private String username;
@Value ( "${jdbc.password}" )
private String password;
@Bean ( name= "jdbcTemplate" )
public JdbcTemplate createJdbcTemplate ( DataSource dataSource) {
return new JdbcTemplate ( dataSource) ;
}
@Bean ( name = "dataSource" )
public DataSource createDataSource ( ) {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource ( ) ;
driverManagerDataSource. setDriverClassName ( driver) ;
driverManagerDataSource. setUrl ( url) ;
driverManagerDataSource. setUsername ( username) ;
driverManagerDataSource. setPassword ( password) ;
return driverManagerDataSource;
}
}
package config ;
import org. springframework. context. annotation. Bean ;
import org. springframework. jdbc. datasource. DataSourceTransactionManager ;
import org. springframework. transaction. PlatformTransactionManager ;
import javax. sql. DataSource ;
public class TransactionConfig {
@Bean ( name = "transactionManager" )
public PlatformTransactionManager createTransactionManager ( DataSource dataSource) {
return new DataSourceTransactionManager ( dataSource) ;
}
}
resource
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root
test
package com. itheima. test ;
import com. itheima. service. IAccountService ;
import config. SpringConfiguration ;
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 ( classes = SpringConfiguration . class )
public class AccountServiceTest {
@Autowired
private IAccountService accountService;
@Test
public void testTransfer ( ) {
accountService. transfer ( "aaa" , "bbb" , 100f ) ;
}
}