項目信息參考 spring 事務管理(一)基於AspectJ的xml方式
和第一種方式唯一不同是使用註解
1.在啓動類添加@EnableTransactionManagement註解,代表啓用事務管理
@SpringBootApplication
@MapperScan("com.zh.springtransaction.mapper")
@EnableTransactionManagement
//@ImportResource(locations={"classpath:application-transaction.xml"})
public class SpringtransactionApplication {
public static void main(String[] args) {
SpringApplication.run(SpringtransactionApplication.class, args);
}
}
2.在需要事務的server層添加@Transactional註解,也可以在方法上面添加@Transactional。
@Service
@Transactional(rollbackFor = java.io.IOException.class)
public class AccountServiceImpl implements IAccountService {
@Autowired
AccountMapper accountMapper;
@Override
public void transfer(String out, String in, double money) {
accountMapper.outMoney(out,money);
int i=1/0;
accountMapper.inMoney(in,money);
}
}