BookServiceImpl implements BookService { @Override @Transactional public AppointExecution appoint(long bookId, long studentId) { try { int update = bookDao.reduceNumber(bookId); if (update <= 0) { throw new NoNumberException("no number"); } else { int insert = appointmentDao.insertAppointment(bookId, studentId); if (insert <= 0) { throw new RepeatAppointException("repeat appoint"); } else { Appointment appointment = appointmentDao.queryByKeyWithBook(bookId, studentId); return new AppointExecution(bookId, AppointStateEnum.SUCCESS, appointment); } } } catch (NoNumberException e1) { throw e1; } catch (RepeatAppointException e2) { throw e2; } catch (Exception e) { logger.error(e.getMessage(), e); throw new AppointException("appoint inner error:" + e.getMessage()); } }  @Override Book getById(long bookId); @Override List<Book> getList(); @Override @Transactional /** * 使用注解控制事务方法的优点： 1.开发团队达成一致约定，明确标注事务方法的编程风格 * 2.保证事务方法的执行时间尽可能短，不要穿插其他网络操作，RPC/HTTP请求或者剥离到事务方法外部 * 3.不是所有的方法都需要事务，如只有一条修改操作，只读操作不需要事务控制 */ AppointExecution appoint(long bookId, long studentId);  }
@Test public void testAppoint() throws Exception { long bookId = 1001; long studentId = 12345678910L; AppointExecution execution = bookService.appoint(bookId, studentId); System.out.println(execution); }