在 DAO、服务层架构中使用 Spring MVC 和 Hibernate 的正确方法是什么
我将 Spring MVC 与 Hibernateaosupport 结合用于我的 DAO 类。这里很困惑从哪里开始事务,是应该在service层还是DAO层?
我的视图与服务层交互。 DAO 被注入到服务中。
在 DAO、服务层架构中使用 Spring MVC 和 Hibernate 的正确方法是什么?
I am using Spring MVC with Hibernatedaosupport
for my DAO classes. Confused here where to start the transaction, whether it should be in service layer or DAO layer?
My view interacts with Service layer. DAO's are injected into services.
What is the right way to use Spring MVC with Hibernate in DAO, service layer architecture?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恕我直言,事务应该进入服务层。通常,一项业务事务由多个查询和更新组成。如果仅将 @Transactional 放置在 DAO 层上,则每个查询和更新都将在单独的事务中运行,这实际上违背了事务的目的。
但如果服务是@Transactional,则每次数据库交互都会加入一个在Web层进入服务层时启动的主事务。请注意,在这种情况下,如果 Web 层运行多个服务方法,则每个服务方法都将在单独的事务中运行(同一问题向上移动一级)。但是将
@Transactional
放在 Web 层中可能会带来意想不到的副作用,例如 N+1 问题,否则这些副作用就会被捕获。因此,尝试将一项业务事务保留在从 Web 层调用的一种服务方法中。IMHO the transactions should go to service layer. Typically one business transaction consists of several queries and updates. If you place
@Transactional
only on DAO layer, each query and update will run in a separate transaction, which effectively defeats the purpose of transactions.But if services are
@Transactional
, each database interaction joins one main transaction started when web layer entered the service layer. Note that in this case if web layer runs several service methods, each of them will run in a separate transaction (the same problem shifted one level up). But placing@Transactional
in web layer might introduce unexpected side effects like N+1 problem, which would have been caught otherwise. Thus try to keep one business transaction in one service method called from web layer.显然是DAO层。连接到数据访问层的任何内容都应该位于 DAO 层中,并且(最好)使用 @Repository 进行注释,并且您的服务(使用 @Service 进行注释)应该具有 DAO 实例的句柄。
如果你要启动一个事务,那么在我看来,它应该在服务层中,这支持了我之前提到的 DAO 本质上应该是原子的声明。
Obviously DAO layer. Anything that connects to data access layer should be in DAO layer and (preferably) annotated with @Repository and your service ( annotated with @Service) should have a handle to DAO instance.
If you are starting a transaction then it should be in service layer in my opinion which supports my previous statement where I mention DAOs should be atomic in nature.
有服务层、DAO 层、实体和控制器的完整信息。它有完整的教程以及每个模块的简短描述。
站点: Spring MVC 与 Hibernate CRUD
或者您可以访问 YouTube 频道:Spring MVC with Hibernate增删改查视频
There is a complete information for the Service layers, DAO layer, Entities and Controllers. It has full tutorial with short description for each module.
Site: Spring MVC With Hibernate CRUD
Or You may visit YouTube Channel: Spring MVC with Hibernate CRUD VIDEO