spring 설명 트랜잭션 이체 사례

16488 단어 토대
spring 설명 트랜잭션 이체 사례
---domain    Account    ;
---dao  
@Component("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
   //      ACCount  
    @Override
    public Account findAccountByName(String sourceName) {
        List<Account> list = jdbcTemplate.query("select * from account where name = ? ", new BeanPropertyRowMapper<Account>(Account.class), sourceName);
       return list.get(0);
    }
	//     
    @Override
    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set money = ? where id = ? ",account.getMoney(),account.getId());
    }
}
--- service 
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
  /******
     *   AccountDao  
     * @param
     * @return
     */
    @Override
    @Transactional(readOnly = false,propagation =Propagation.REQUIRED)
    public void transfer(String sourceName, String targeName, Float money) {
        //1.          
        Account source = accountDao.findAccountByName(sourceName);
        Account target = accountDao.findAccountByName(targeName);
        //2.         
        source.setMoney(source.getMoney()-money);//      
        target.setMoney(target.getMoney()+money);//      
        //3.      
        accountDao.updateAccount(source);
        //       。
        int i =10/0;
        accountDao.updateAccount(target);
    }
}
--- SpringConfig    
@Configuration
@ComponentScan("com.hupu")
@EnableTransactionManagement
public class SpringConfig {
    //  jdbcTemplate        
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate =new  JdbcTemplate(dataSource);
        return jdbcTemplate;
    }
    //  dataSource            
    @Bean
    public DataSource dataSource(){
        ComboPooledDataSource dataSource =null;
        try {
            dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setPassword("root");
            dataSource.setUser("root");
            dataSource.setJdbcUrl("jdbc:mysql:///hupu55");
        } catch (Exception e) {
            e.printStackTrace();
        }
       return dataSource;
    }
    
    //  transactionManager               
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager dsm =new  DataSourceTransactionManager();
        dsm.setDataSource(dataSource);

        return dsm;

    }
}


---       
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        
  ---test     
@RunWith(SpringRunner.class)
@ContextConfiguration(classes =SpringConfig.class )
public class Springtest02 {

    @Autowired
    private  AccountService accountService;
    @Test
    public void test02() {
        accountService.transfer("aaa","bbb",100f);

    }
}

좋은 웹페이지 즐겨찾기