@Transactional(rollbackFor = java.lang.Exception.class) @Override public Map<String, String> test() { Map<String, String> map = new HashMap<>(); int i = testDao.save("name5"); try { String str = null ; if (i == 1) { str.equals(""); //保存成功,制造null异常 } testDao.save("name6"); } catch (Exception e) { e.printStackTrace(); map.put("data","失败"); //当异常被catch后,spring捕捉不到,因而不能回滚事务 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚事务 return map ; //throws new Exception("自定义异常"); //或者throws 异常 } map.put("data","成功!"); return map; }