@Autowired private DataSourceTransactionManager txManager; //方法內的事務控制 DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = txManager.getTransaction(def); try { userMapper.insertUser(user); } catch (MyException ex) { txManager.rollback(status); throw ex; } txManager.commit(status);