事务
# 事务
- 给开启事务的函数添加
@Transactional
- 在配置类中开启
@EnableTransactionManagement
- 在jdbcConfig中配置事务
以下是银行转账的经典案例
public interface AccountService {
@Transactional
void transfer(String in,String out,int money);
}
1
2
3
4
2
3
4
@Configuration
@ComponentScan("com.project")
@Import({jdbcConfig.class,MyBatisConfig.class})
@PropertySource("jdbc.properties")
@EnableTransactionManagement
public class SpringConfig {
}
1
2
3
4
5
6
7
2
3
4
5
6
7
//jdbcConfig.class
@Bean
public PlatformTransactionManager getPlatformTransactionManager(DataSource dataSource){
DataSourceTransactionManager dtm = new DataSourceTransactionManager();
dtm.setDataSource(dataSource);
return dtm;
}
1
2
3
4
5
6
7
2
3
4
5
6
7