总配置类SpringConfiguration:
@Configuration
@ComponentScan("net.togogo")
@Import({JdbcConfig.class,TransactionConfig.class})
@PropertySource("classpath:jdbcConfig.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
jdbcConfig.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy
jdbc.username=root
jdbc.password=123456
JdbcConfig类:
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;
}
}
TransactionConfig
public class TransactionConfig {
@Bean(name = "transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
AccountServiceImpl:
package net.togogo.service.impl;
import net.togogo.bean.Account;
import net.togogo.dao.IAccountDao;
import net.togogo.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;
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public Account findAccountById(Integer id) {
return accountDao.findAccountById(id);
}
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
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);
int i=1/0;
accountDao.updateAccount(target);
}
}
AccountDaoImpl:
package net.togogo.dao.impl;
import net.togogo.bean.Account;
import net.togogo.dao.IAccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public Account findAccountById(Integer id) {
List<Account> accountList = jdbcTemplate.query("select * from account1 where id = ?", new BeanPropertyRowMapper<Account>(Account.class), id);
return accountList.isEmpty()?null:accountList.get(0);
}
public Account findAccountByName(String name) {
List<Account> accountList = jdbcTemplate.query("select * from account1 where username = ?", new BeanPropertyRowMapper<Account>(Account.class), name);
if(accountList.isEmpty()){
return null;
}
if(accountList.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accountList.get(0);
}
public void updateAccount(Account account) {
jdbcTemplate.update("update account1 set username=?,money=? where id=?",account.getUserName(),account.getMoney(),account.getId());
}
}
测试类
package net.togogo;
import config.SpringConfiguration;
import net.togogo.bean.Account;
import net.togogo.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class Demo_Test_Spring {
@Autowired
private IAccountService accountService;
@Test
public void testTransfer(){
accountService.transfer("aaa","bbb",100f);
}
}